是說有多個(gè)三目運(yùn)算符在一個(gè)表達(dá)式里的時(shí)候,從右向左執(zhí)行
目前創(chuàng)新互聯(lián)已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、和碩網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
a b ? b c ? 1 : 0 : 2會(huì)先執(zhí)行b c ? 1 : 0
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PoetryTest {
public static void main(String[] args) throws Exception {
final JFrame f = new JFrame();
final ArrayListPoetry ps = PContents.load();
final PoetryLabel plabel = new PoetryLabel();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(plabel);
f.setSize(320,268);
f.setLocationRelativeTo(null);
f.setVisible(true);
plabel.setPoetry(ps.get(0));
f.setTitle(plabel.getTitle()+"-"+plabel.getAuthor());
plabel.addMouseListener(new MouseAdapter(){
int index;
public void mouseClicked(MouseEvent e){
++index;
index=index%ps.size();
Poetry p = ps.get(index);
plabel.setPoetry(p);
f.setTitle(plabel.getTitle()+"-"+plabel.getAuthor());
}
});
}
}
//呈現(xiàn)詩歌的標(biāo)簽類
class PoetryLabel extends JLabel{
private static final Color tColor = Color.white;//標(biāo)題顏色
private static final Color aColor = Color.yellow;//作者顏色
private static final Color cColor = Color.LIGHT_GRAY;//內(nèi)容顏色
private static final int fontSize=16;//字體大小
private static final int tType = Font.BOLD;//標(biāo)題字體樣式為粗體
private static final int aType = Font.ITALIC;//作者字體樣式為斜體
private static final int cType = Font.PLAIN;//內(nèi)容字體樣式為普通樣式
private static final float scaleFactor = 1.2f;//標(biāo)題字體大小比其它字體大
private static final String fontName="SimSun";//字體名稱
private static final int offset=5,space=5;
private Font font;
private AffineTransform at;
private Poetry poem;
public PoetryLabel(){
this.poem=new Poetry();
this.font=new Font(fontName,cType,fontSize);
this.setHorizontalAlignment(JLabel.CENTER);
this.setVerticalAlignment(JLabel.CENTER);
at = new AffineTransform();
at.setToRotation(Math.toRadians(-90));
font = font.deriveFont(at);//將字體逆時(shí)針旋轉(zhuǎn)90°
}
public Poetry getPoetry() {
return poem;
}
//設(shè)置詩歌,來初始化標(biāo)簽內(nèi)容
public void setPoetry(Poetry s){
this.poem=s;
this.repaint();
}
public String getTitle(){return poem.title;}
public String getAuthor(){return poem.author;}
//創(chuàng)建圖像,先將逆時(shí)針旋轉(zhuǎn)了90°的文件畫上,然后將整幅圖像順時(shí)針旋轉(zhuǎn)90°
private Image createImage(){
int x;
int y=offset;
int w = getTWidth(poem);
int h = getTHeight(poem);
BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
//draw title
g.setColor(tColor);
Font tfont = font.deriveFont(font.getSize()*scaleFactor).deriveFont(tType);
g.setFont(tfont);
y+=tfont.getSize();
char[] cs = poem.title.toCharArray();
for(int i=0; ics.length; i++){
x=(i+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, i, 1, x, y);
}
//draw author
y+=space*2+tfont.getSize();
cs = poem.author.toCharArray();
tfont = font.deriveFont(aType);
g.setColor(aColor);
g.setFont(tfont);
for(int i=0; ics.length; i++){
x=(i+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, i, 1, x, y);
}
//draw contents
y+=space*3;
g.setColor(cColor);
g.setFont(font);
for(int i=0; ipoem.contents.size(); i++){
y+=fontSize+space;
cs = poem.contents.get(i).toCharArray();
for(int j=0; jcs.length; j++){
x=(j+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, j, 1, x, y);
}
}
g.dispose();
return this.getRotateImage(img);
}
//將圖像順時(shí)針旋轉(zhuǎn)90°
private Image getRotateImage(final BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage newImg = new BufferedImage(h,w,2);
AffineTransformOp op = new AffineTransformOp(AffineTransform.
getRotateInstance(Math.toRadians(90),h/2,h/2),null);
return op.filter(img,newImg);
}
//繪圖
public void paint(Graphics gg){
BufferedImage img = (BufferedImage)this.createImage();
int iw = img.getWidth();
int ih = img.getHeight();
Graphics2D g = (Graphics2D)gg;
int w = this.getWidth();
int h = this.getHeight();
g.setPaint(new GradientPaint(0,0,Color.DARK_GRAY,0,h,Color.black));
g.fillRect(0,0,w,h);
int x = (w-iw)/2;
int y = (h-ih)/2;
g.drawImage(img, x, y, this);
g.dispose();
}
//計(jì)算圖片的高度
private int getTHeight(Poetry s) {
int h = offset*2+space*5;
h+=(int)(fontSize*(scaleFactor+1));
h+=(fontSize+space*2)*s.contents.size();
return h;
}
//計(jì)算圖片的寬度
private int getTWidth(Poetry s) {
int tl = (int)(s.title.length()*(fontSize*scaleFactor+space)+offset*2);
int al = s.author.length()*(fontSize+space)+offset*2;
int t;
int max = tlal?tl:al;
for(int i=0; is.contents.size(); i++){
t = s.contents.get(i).length()*(fontSize+space)+offset*2;
max=maxt?max:t;
}
return max;
}
}
//詩歌類
class Poetry{
public String title;//標(biāo)題
public String author;//作者
public ArrayListString contents=new ArrayListString();//內(nèi)容
public void addLine(String s){this.contents.add(s);}//加入一行內(nèi)容
}
//
class PContents{
public static String s=
"蘇武廟\r\n"+
"溫庭筠\r\n"+
"蘇武魂銷漢使前,\r\n"+
"古祠高樹兩茫然。\r\n"+
"云邊雁斷胡天月,\r\n"+
"隴上羊歸塞草煙。\r\n"+
"回日樓臺(tái)非甲帳,\r\n"+
"去時(shí)冠劍是丁年。\r\n"+
"茂陵不見封侯印,\r\n"+
"空向秋波哭逝川。\r\n"+
"\r\n"+
"貧女\r\n"+
"秦韜玉\r\n"+
"蓬門未識(shí)綺羅香,\r\n"+
"擬托良媒益自傷。\r\n"+
"誰愛風(fēng)流高格調(diào)?\r\n"+
"共憐時(shí)世儉梳妝。\r\n"+
"敢將十指夸針巧,\r\n"+
"不把雙眉斗畫長。\r\n"+
"苦恨年年壓金線,\r\n"+
"為他人作嫁衣裳。\r\n"+
"\r\n"+
"宮詞\r\n"+
"薛逢\r\n"+
"十二樓中盡曉妝,\r\n"+
"望仙樓上望君王。\r\n"+
"鎖銜金獸連環(huán)冷,\r\n"+
"水滴銅龍晝漏長。\r\n"+
"云髻罷梳還對(duì)鏡,\r\n"+
"羅衣欲換更添香。\r\n"+
"遙窺正殿簾開處,\r\n"+
"袍褲宮人掃御床。\r\n"+
"\r\n"+
"利洲南渡\r\n"+
"溫庭筠\r\n"+
"澹然空水對(duì)斜暉,\r\n"+
"曲島蒼茫接翠微。\r\n"+
"波上馬嘶看棹去,\r\n"+
"柳邊人歇待船歸。\r\n"+
"數(shù)叢沙草群鷗散,\r\n"+
"萬頃江田一鷺飛。\r\n"+
"誰解乘舟尋范蠡,\r\n"+
"五湖煙水獨(dú)忘機(jī)?\r\n"+
"\r\n"+
"無題二首之一\r\n"+
"李商隱\r\n"+
"鳳尾香羅薄幾重,\r\n"+
"碧文圓頂夜深縫。\r\n"+
"扇裁月魄羞難掩,\r\n"+
"車走雷聲語未通。\r\n"+
"曾是寂寥金燼暗,\r\n"+
"斷無消息石榴紅。\r\n"+
"斑騅只系垂楊岸,\r\n"+
"何處西南任好風(fēng)?\r\n"+
"\r\n"+
"無題二首之二\r\n"+
"李商隱\r\n"+
"重帷深下莫愁堂,\r\n"+
"臥后清宵細(xì)細(xì)長。\r\n"+
"神女生涯原是夢(mèng),\r\n"+
"小姑居處本無郎。\r\n"+
"風(fēng)波不信菱枝弱,\r\n"+
"月露誰教桂葉香?\r\n"+
"直道相思了無益,\r\n"+
"未妨惆悵是清狂。\r\n"+
"\r\n"+
"金陵圖\r\n"+
"韋莊\r\n"+
"江雨霏霏江草齊,\r\n"+
"六朝如夢(mèng)鳥空啼。\r\n"+
"無情最是臺(tái)城柳,\r\n"+
"依舊煙籠十里堤。\r\n" +
"\r\n"
;
public static ArrayListPoetry load() throws Exception{
ArrayListPoetry list = new ArrayListPoetry();
BufferedReader br = new BufferedReader(new StringReader(s));
Poetry p = null;
String t = null;
while((t = br.readLine()) != null){
if(p==null)
p=new Poetry();
if(t.isEmpty()){
list.add(p);
p=null;
}
else if(p.title==null)
p.title=t;
else if(p.author==null)
p.author=t;
else
p.addLine(t);
}
return list;
}
}
/*?字符串串從右往左截取?
*?思路:
*? 讓字符串倒序,然后再截取
*?*/
public?static?String?spiltRtoL(String?s){
StringBuffer?sb?=?new?StringBuffer();
int?length?=?s.length();
char[]?c?=?new?char[length];
for?(int?i?=?0;?i??length;?i++)?{
c[i]?=?s.charAt(i);
}
for?(int?i?=?length?-?1;?i?=?0;?i--)?{
sb.append(c[i]);
}
return?sb.toString();
}
你的左右沒分清楚
如果不用正則表達(dá)式,就用int i = str.lastIndexOf("/n");能找出位置來
如果用正則表達(dá)式,就用"(/n)(?=(^/n)*)"意思就是匹配后面再?zèng)]有/n的/n
本文名稱:java古詩代碼從右到左 java從左到右取數(shù)字
網(wǎng)址分享:http://jinyejixie.com/article16/ddojigg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站改版、響應(yīng)式網(wǎng)站、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)