我有包含GUI的此活動。此活動包括輸入長度和寬度,x和y(畫布中形狀的位置)以及選擇形狀,矩形或圓形的按鈕。點擊按鈕後,它接受文本字段內的長度,寬度,x和y。Java - JFrame,JPanel和組件不會顯示
手動擺弄GUI代碼後,JPanels和JFrames即使在設置大小後也不會顯示,只是純粹的空白。
我期待將畫布放在左側,標籤,文本框和按鈕位於右側。我究竟做錯了什麼?我仍然處於該項目的早期階段,只是美學。謝謝。
public class Problem02{
Problem02(){
JFrame framer = new JFrame("Problem 02");
framer.setLayout(new FlowLayout());
framer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel LPanel = new JPanel();
JPanel RPanel = new JPanel();
Canvas canvasses = new Canvas();Label XLabel = new Label("X");
JLabel YLabel = new JLabel("Y");
JLabel WidthLabel = new JLabel("Width");
JLabel LengthLabel = new JLabel("Length");
JButton RectangleButton = new JButton("Rectangle");
JButton CircleButton = new JButton("Circle");
JTextField XText = new JTextField("");
JTextField YText = new JTextField("");
JTextField WidthText = new JTextField("");
JTextField LengthText = new JTextField("");
framer.add(LPanel, BorderLayout.WEST);
framer.add(RPanel, BorderLayout.EAST);
LPanel.add(canvasses);
RPanel.add(XLabel);
RPanel.add(XText);
RPanel.add(YLabel);
RPanel.add(YText);
RPanel.add(WidthLabel);
RPanel.add(WidthText);
RPanel.add(LengthLabel);
RPanel.add(LengthText);
RPanel.add(RectangleButton);
RPanel.add(CircleButton);
framer.setSize(500,500);
framer.setTitle("Problem 02");
framer.setVisible(true);
}
public static void main(String[] args) {
new Problem02();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowClosing(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowClosed(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowIconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowDeiconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowActivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void windowDeactivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
把setVisible(true)放在你的主要方法中。新的Problem02.setVisible(true); –
也應該將您的JPanel添加到您的JFrame中以查看添加的組件。使用:framer.add(xPanel); –
我已經做了framer.add(LPanel);和framer.add(RPanel); – user3255372