2012-11-19 14 views
1

我想讓我的程序添加更多行內容,最終將允許它們輸入點值(x & y)。但是,我似乎無法讓我的JPanel在添加按鈕被按下時添加任何東西。似乎不能讓現有的JPanel添加更多JPanel

這裏是我試圖添加到我的整體的JPanel類(JPanel的擴展名):

class Coordinates extends JPanel 
{ 
    public String x; 
    public String y; 
    public int id; 

    public Coordinates(int spot) 
    { 
    super(); 
    x = "0"; 
    y = "0"; 
    id = spot; 
    setLayout(null); 
    setSize(440,30); 
    setLocation(10,5); 
    JLabel num = new JLabel("Point #" + (id + 1) + ":"); 
    num.setLocation(0,0); 
    num.setSize(40,20); 
    add(num); 
    } 
} 

這裏是AbstractAction擴展添加按鈕按下時應該添加:

class AddACT extends AbstractAction 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
    pointList.add(new Point()); 
    gui.add(new Coordinates(1)); 
    } 
} 

而且,如果任何人彌補的代碼龐大塊,這裏是整個窗口的代碼:

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

public class PointWindow 
{ 
    private ArrayList<Point> pointList = new ArrayList<Point>(); 

    public PointWindow() 
    { 
    initialize(); 
    } 

    public void initialize() 
    { 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("Set New Points"); 
    frame.setContentPane(createGUI()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(440,600); 
    frame.setLocationRelativeTo(null); 
    frame.setResizable(false); 
    frame.setVisible(true); 
    } 

    public JPanel createGUI() 
    { 
    final JPanel gui = new JPanel(); 
    gui.setLayout(null); 

    Font boldTitle = new Font("Arial", Font.BOLD, 30); 

    class Line extends JPanel 
    { 
     protected void paintComponent(Graphics g) 
     { 
     super.paintComponent(g); 
     g.drawLine(0,0,430,0); 
     } 
    } 

    class Coordinates extends JPanel 
    { 
     public String x; 
     public String y; 
     public int id; 

     public Coordinates(int spot) 
     { 
     super(); 
     x = "0"; 
     y = "0"; 
     id = spot; 
     setLayout(null); 
     setSize(440,30); 
     setLocation(10,5); 
     JLabel num = new JLabel("Point #" + (id + 1) + ":"); 
     num.setLocation(0,0); 
     num.setSize(40,20); 
     add(num); 
     } 
    } 

    final JButton add = new JButton("Add"); 
    final JButton remove = new JButton("Remove"); 
    final JButton makeGraph = new JButton("Generate Graph!"); 

    final JLabel numPointsL = new JLabel("0"); 

    class AddACT extends AbstractAction 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
     pointList.add(new Point()); 
     gui.add(new Coordinates(1)); 
     } 
    } 

    class RemoveACT extends AbstractAction 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    } 

    class MakeGraphACT extends AbstractAction 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    } 

    JPanel line1 = new JPanel(); 
    line1.setLayout(null); 
    line1.setLocation(5,5); 
    line1.setSize(440,35); 
    gui.add(line1); 

    JLabel text1 = new JLabel("Point Manager"); 
    text1.setFont(boldTitle); 
    text1.setLocation(0,0); 
    text1.setSize(300,35); 
    line1.add(text1); 

    add.setLocation(220,0); 
    add.setSize(100,33); 
    add.addActionListener(new AddACT()); 
    line1.add(add); 

    remove.setLocation(330,0); 
    remove.setSize(100,33); 
    add.addActionListener(new RemoveACT()); 
    line1.add(remove); 

    JPanel line2 = new JPanel(); 
    line2.setLayout(null); 
    line2.setLocation(5,45); 
    line2.setSize(440,30); 
    gui.add(line2); 

    JLabel text2 = new JLabel("Current number of points:"); 
    text2.setLocation(2,1); 
    text2.setSize(165,20); 
    line2.add(text2); 

    numPointsL.setLocation(172,1); 
    numPointsL.setSize(40,20); 
    line2.add(numPointsL); 

    makeGraph.setLocation(222,0); 
    makeGraph.setSize(206,22); 
    makeGraph.addActionListener(new MakeGraphACT()); 
    line2.add(makeGraph); 

    Line l1 = new Line(); 
    l1.setLocation(5,29); 
    l1.setSize(420,1); 
    line2.add(l1); 

    return gui; 
    } 
} 
+0

不用擔心Point類,它還沒有完全編碼。 – Thrfoot

+0

簡單,使用合適的佈局管理器 – MadProgrammer

回答

3

他們開始添加,但它開始添加在其他組件之後。

而不是使用setLocation(10,5)嘗試setLocation(5, 75),這將把它放在線下。

此外,調用gui.repaint()add電話後,要求該UI重繪

更重要的是,使用佈局管理器。

+0

試過了,什麼都沒有發生。我會看看這些佈局管理器,但... – Thrfoot

+1

請參閱此[圖庫](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)。 – trashgod

+0

好的,所以我必須使用佈局管理器將事物添加到現有的JPanel中?有沒有什麼特別的原因,我不能只使用add()方法,還是隻是像java中那樣工作? – Thrfoot