2014-10-31 44 views
0

我想添加三個矩形到BorderLayout的中心,我完全失去了。我完成的程序需要在滑塊移動時增加矩形的高度,但我試圖弄清楚如何將這三個矩形繪製到jpanel中。我很迷茫。我的代碼如下。如何在java中爲Jpanel添加3個矩形?

import java.awt.*; 
import javax.swing.*; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class ShowColors extends JPanel 
{ 

public static void main(String args[]) 
{ 
    JFrame frame = new JFrame(); 

    JPanel main = new JPanel(new BorderLayout()); 
    main.setSize(2000, 1000); 
    frame.setContentPane(main); 

    JPanel jp1 = new JPanel(new GridLayout(0, 1)); 
    JPanel jp2 = new JPanel(new GridLayout(2,3)); 
    JPanel jp3 = new JPanel(new GridLayout(1, 3)); 

    jp1.setPreferredSize(new Dimension(90, 800)); 
    jp2.setPreferredSize(new Dimension(1000, 150)); 
    jp3.setPreferredSize(new Dimension(800, 600)); 

    JRadioButton rb1 = new JRadioButton("Decimal", true); 
    JRadioButton rb2 = new JRadioButton("Binary"); 
    JRadioButton rb3 = new JRadioButton("Hex"); 
    JRadioButton rb4 = new JRadioButton("Octal"); 
    JButton jb1 = new JButton("RESET"); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(rb1); 
    group.add(rb2); 
    group.add(rb3); 
    group.add(rb4); 

    JSlider jRed = new JSlider(0,255); 
    JSlider jGreen = new JSlider(0,255); 
    JSlider jBlue = new JSlider(0,255); 

    jRed.setPaintLabels(true); 
    jRed.setPaintTicks(true); 
    jRed.setMinorTickSpacing(5); 
    jRed.setMajorTickSpacing(50); 
    jRed.setValue(0); 

    jGreen.setPaintLabels(true); 
    jGreen.setPaintTicks(true); 
    jGreen.setMinorTickSpacing(5); 
    jGreen.setMajorTickSpacing(50); 
    jGreen.setValue(0); 

    jBlue.setPaintLabels(true); 
    jBlue.setPaintTicks(true); 
    jBlue.setMinorTickSpacing(5); 
    jBlue.setMajorTickSpacing(50); 
    jBlue.setValue(0); 

    JLabel labelR = new JLabel("Red", JLabel.CENTER); 
    JLabel labelG = new JLabel("Green", JLabel.CENTER); 
    JLabel lableB = new JLabel("Blue", JLabel.CENTER); 

    jp1.add(rb1); 
    jp1.add(rb2); 
    jp1.add(rb3); 
    jp1.add(rb4); 
    jp1.add(jb1); 

    jp2.add(labelR); 
    jp2.add(labelG); 
    jp2.add(lableB); 
    jp2.add(jRed); 
    jp2.add(jGreen); 
    jp2.add(jBlue); 

    main.add(jp1, BorderLayout.WEST); 
    main.add(jp2, BorderLayout.SOUTH); 

    frame.pack(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

} 
public void paint(Graphics g) 
{ 
    super.paint(g); 
    g.drawRect(0, 0, 10, 20); 
    g.setColor(Color.RED); 
    g.fillRect(0, 0, 10, 20); 

    g.drawRect(10, 0, 10, 20); 
    g.setColor(Color.GREEN); 
    g.fillRect(10, 0, 10, 20); 

    g.drawRect(20, 0, 10, 20); 
    g.setColor(Color.BLUE); 
    g.fillRect(20, 0, 10, 20); 
} 

} 

這是我的佈局,我想在中心的矩形。 sample output

回答

1

我想你只需要的JPanel一個簡單的子類,並覆蓋paintComponent()。像這樣的東西應該讓你去:

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

public class Canvas extends JPanel { 

    // TODO member variables for rectangle size/color 

    public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.fillRect(10,10,100,50); 
    g.drawRect(10,80,100,50); 
    } 
} 

編輯: 其實,我猜你真的不需要一個「畫布」類,你可以使用一個簡單的JPanel作爲@MadProgrammer建議。你需要的是一個封裝了矩形行爲的類,它只是一個簡單的JComponent,它被添加到包含你的三個矩形的JPanel中。

這裏是一個有效的解決方案,進口排除簡潔:

public class ShowColors { 

    class Rectangle extends JComponent implements ChangeListener { 
     private JSlider slider; 
     private Color color; 

     public Rectangle(JSlider slider, Color color) { 
      this.slider = slider; 
      this.color = color; 
      this.setPreferredSize(new Dimension(250, 250)); 
      slider.addChangeListener(this); 
     } 

     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      int value = slider.getValue(); 
      g.setColor(color); 
      g.fillRect(10,10,100,value); 

     } 

     @Override 
     public void stateChanged(ChangeEvent arg0) { 
      this.repaint(); 
     } 
    } 

    public ShowColors() { 
     JFrame frame = new JFrame(); 

     JPanel main = new JPanel(new BorderLayout()); 
     main.setSize(2000, 1000); 
     frame.setContentPane(main); 

     JPanel jp1 = new JPanel(new GridLayout(0, 1)); 
     JPanel jp2 = new JPanel(new GridLayout(2, 3)); 

     jp1.setPreferredSize(new Dimension(90, 800)); 
     jp2.setPreferredSize(new Dimension(1000, 150)); 

     JRadioButton rb1 = new JRadioButton("Decimal", true); 
     JRadioButton rb2 = new JRadioButton("Binary"); 
     JRadioButton rb3 = new JRadioButton("Hex"); 
     JRadioButton rb4 = new JRadioButton("Octal"); 
     JButton jb1 = new JButton("RESET"); 

     ButtonGroup group = new ButtonGroup(); 
     group.add(rb1); 
     group.add(rb2); 
     group.add(rb3); 
     group.add(rb4); 

     JSlider jRed = buildSlider(); 
     JSlider jGreen = buildSlider(); 
     JSlider jBlue = buildSlider(); 

     JLabel labelR = new JLabel("Red", JLabel.CENTER); 
     JLabel labelG = new JLabel("Green", JLabel.CENTER); 
     JLabel lableB = new JLabel("Blue", JLabel.CENTER); 

     jp1.add(rb1); 
     jp1.add(rb2); 
     jp1.add(rb3); 
     jp1.add(rb4); 
     jp1.add(jb1); 

     jp2.add(labelR); 
     jp2.add(labelG); 
     jp2.add(lableB); 
     jp2.add(jRed); 
     jp2.add(jGreen); 
     jp2.add(jBlue); 

     JPanel canvas = new JPanel(); 
     canvas.setLayout(new FlowLayout()); 
     canvas.setPreferredSize(new Dimension(800, 600)); 
     canvas.add(new Rectangle(jRed, Color.RED)); 
     canvas.add(new Rectangle(jGreen, Color.GREEN)); 
     canvas.add(new Rectangle(jBlue, Color.BLUE)); 

     main.add(jp1, BorderLayout.WEST); 
     main.add(jp2, BorderLayout.SOUTH); 
     main.add(canvas, BorderLayout.EAST); 

     frame.pack(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private static JSlider buildSlider() { 
     JSlider slider = new JSlider(0, 255); 
     slider.setPaintLabels(true); 
     slider.setPaintTicks(true); 
     slider.setMinorTickSpacing(5); 
     slider.setMajorTickSpacing(50); 
     slider.setValue(50); 
     return slider; 
    } 

    public static void main(String args[]) { 
     new ShowColors(); 
    } 

} 

下面是什麼樣子:

enter image description here

+0

所以使用這個類,我創建畫布對象主,並將其添加到jpanel? – user3242607 2014-10-31 22:02:05

+0

是的,這是一般的想法。 – helmy 2014-10-31 22:05:19

+0

@ user3242607不,'Canvas'和'JPanel'不喜歡一起玩,最終會出現繪畫工件和z順序問題。你不應該混合沉重的(AWT)和光(Swing)組件,這是不值得的。如果您要開始向當前的'JPanel'添加組件,那麼爲什麼您需要自定義繪畫? – MadProgrammer 2014-11-01 00:56:04

1
  1. 不要覆蓋paint,畫在Swing是由方法一個微妙而複雜的鏈條,這是很容易折斷實現。相反,重寫它的paintComponent方法。請參閱Painting in AWT and Swing以瞭解更多詳細信息
  2. 話雖如此,不要將所有組件添加到要繪製到的同一個面板中,最終將繪製所有組件。相反,創建一個單獨的JPanel作爲油漆表面和另一個作爲控制器(包含控件和油漆表面)的JPanel。使用setters和getters來改變油漆表面的狀態。有關更多詳細信息,請參閱Performing Custom Painting
  3. 創建某種「繪製」對象,它知道如何繪製自己以及它應該用油漆本身(顏色)的
  4. 創建某種List(在車漆表面類),可容納的對象你想繪畫。在它的paintComponent中,您將循環訪問列表並請求每個對象繪製自己,並將其傳遞給Graphics上下文。看看2D Graphics更多細節

previous answer還可以幫助