我是从头开始做 JAVA Swing GUI 而不是拖放的新手。您能告诉我如何调整代码以显示我想要的输出吗?
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener {
private JFrame mainFrame;
private JPanel upperPanel, lowerPanel;
private JLabel header, lblName, lblPrice, lblQty;
private JTextField txtName, txtPrice, txtQty;
public GUI(){
prepareGUI();
}
private void prepareGUI(){
mainFrame = new JFrame("CRUD PROJECT");
mainFrame.setSize(750, 750);
mainFrame.setLayout(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setLayout(new GridLayout(3, 1));
upperPanel= new JPanel();
upperPanel.setBackground(Color.red);
upperPanel.setBounds(0,0, 750, 360);
lowerPanel= new JPanel();
lowerPanel.setBackground(Color.blue);
lowerPanel.setBounds(0,360, 750, 360);
header = new JLabel();
header.setText("JAVA CRUD");
header.setFont(new Font("Tahoma", Font.BOLD, 30));
header.setBounds(300, 3, 250, 100);
//header.setOpaque(true);
//header.setBackground(Color.blue);
lblName = new JLabel();
lblName.setText("Product Name");
lblName.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblName.setBounds(10, 110, 200, 50);
// lblName.setOpaque(true);
//lblName.setBackground(Color.blue);
txtName= new JTextField();
lblName.setBounds(25, 110, 200, 50);
txtName.setPreferredSize(new Dimension(250,40));
upperPanel.add(header);
upperPanel.add(lblName);
upperPanel.add(txtName);
mainFrame.add(upperPanel);
mainFrame.add(lowerPanel);
}
public static void main(String[] args) throws Exception {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
我正在尝试创建一个 CRUD GUI,其中 JLabel 旁边会有一个文本字段。由于某种原因,它没有显示。