2012-08-28 39 views
0

我怎麼能在左側出現「畫線,矩形和橢圓形」?即時通訊目前在左上角,我不知道該怎麼做?請幫助java中的狀態欄?

我也試着和它沒有工作像

的JLabel標籤=新的JLabel( 「文本標籤」,JLabel.LEFT);

label.setVerticalAlignment(JLabel.BOTTOM);

import java.awt.*; 
import javax.swing.*; 

public class LinesRectsOvalsJPanel extends JPanel { 


public void paintComponent(Graphics g) { 



    super.paintComponent(g); 

    this.setBackground(Color.WHITE); 



    // x y width height 
    g.setColor(Color.BLACK); 
    g.drawLine(5,10,5,30); 
    g.setColor(Color.BLUE); 
    g.drawLine(18,70,127,24); 
    g.setColor(Color.RED); 
    g.drawLine(25,45,100,38); 

    g.setColor(Color.YELLOW); 
    g.drawOval(23,25,23,55); 
    g.setColor(Color.BLACK); 
    g.drawOval(15,14,40,78); 
    g.setColor(Color.CYAN); 
    g.drawOval(180,102,5,90); 
    g.setColor(Color.RED); 
    g.drawOval(21,20,89,11); 
    g.setColor(Color.BLUE); 
    g.drawOval(35,87,39,27); 


    g.setColor(Color.YELLOW); 
    g.fillRect(87,5,5,60); 

    g.setColor(Color.GREEN); 
    g.fillRect(105,15,15,85); 

    g.setColor(Color.CYAN); 
    g.fillRect(14,45,76,86); 

    g.setColor(Color.RED); 
    g.fillRect(70,79,65,86); 

    g.setColor(Color.BLUE); 
    g.fillRect(90,108,5,8); 

     } 

    } 



    import java.awt.*; 
    import javax.swing.*; 

    public class LinesRectsOvals { 

    public static void main(String args[]) { 

    JFrame frame = 
     new JFrame("Drawing lines, rectangles and ovals"); 

     LinesRectsOvalsJPanel linesRectsOvalsJPanel = 
     new LinesRectsOvalsJPanel(); 

     linesRectsOvalsJPanel.setBackground(Color.WHITE); 
     frame.add(linesRectsOvalsJPanel); // add panel to frame 
     frame.setSize(300, 300); // set frame size 
     frame.setVisible(true); 
     } 
     } 

回答

1

您用於繪製線條的x和y值相對於屏幕的左上角。要參考底角使用低x和大y值。我希望這有幫助。

0

指數原則上是正確的。在實踐中,你會想知道你正在繪製的容器的高度和寬度。

試試這個

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    this.setBackground(Color.WHITE); 

    int width = getWidth() - 1; 
    int height = getHeight() - 1; 

    // x y width height 
    g.setColor(Color.BLACK); 
    g.drawLine(5, height - 30 - 10, 5, 30); 
    g.setColor(Color.BLUE); 
    g.drawLine(18, height - 24 - 70, 127, 24); 
    g.setColor(Color.RED); 
    g.drawLine(25, height - 38 - 45, 100, 38); 

    g.setColor(Color.YELLOW); 
    g.drawOval(23, height - 55 - 25, 23, 55); 
    g.setColor(Color.BLACK); 
    g.drawOval(15, height - 78 - 14, 40, 78); 
    g.setColor(Color.CYAN); 
    g.drawOval(180, height - 90 - 102, 5, 90); 
    g.setColor(Color.RED); 
    g.drawOval(21, height - 11 - 20, 89, 11); 
    g.setColor(Color.BLUE); 
    g.drawOval(35, height - 27 - 87, 39, 27); 


    g.setColor(Color.YELLOW); 
    g.fillRect(87, height - 50 - 5, 5, 60); 

    g.setColor(Color.GREEN); 
    g.fillRect(105, height - 85 - 15, 15, 85); 

    g.setColor(Color.CYAN); 
    g.fillRect(14, height - 86 - 45, 76, 86); 

    g.setColor(Color.RED); 
    g.fillRect(70, height - 86 - 79, 65, 86); 

    g.setColor(Color.BLUE); 
    g.fillRect(90, height - 8 - 108, 5, 8); 

} 

在這裏重要的部分是

int width = getWidth() - 1; 
int height = getHeight() - 1; 
0

我會先經過你的代碼來解釋所發生的事情:

JLabel label = new JLabel("Text Label", JLabel.LEFT); 
label.setVerticalAlignment(JLabel.BOTTOM); 

setVerticalAlignment隻影響如何標籤文本位於標籤內部,而不是標籤如何定位e父容器。請參閱該方法的javadoc

設置標籤內容沿Y軸的對齊方式。

,你的「畫線......」字符串出現在頂部的事實是,因爲下面的代碼

JFrame frame = new JFrame("Drawing lines, rectangles and ovals"); 

這產生了您指定的幀標題的新框架。就像所有其他程序一樣,標題顯示在頂部(以及最大化,關閉和最小化按鈕)。

如果你想在底部有文字,你可以簡單地使用適當的佈局管理器。

JFrame frame = new JFrame("Whatever title you want"); 
JPanel contentPane = new JPanel(new BorderLayout()); 

LinesRectsOvalsJPanel linesRectsOvalsJPanel = 
     new LinesRectsOvalsJPanel(); 
contentPane.add(linesRectsOvalsJPanel, BorderLayout.CENTER); 

Component statusBar = ...;//probably a JLabel is sufficient 
contentPane.add(statusBar, BorderLayout.SOUTH); 

更多關於佈局管理器和實例鏈接可以在Swing info page找到。