我有包含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