我在 JPanel 中添加组件时遇到了一些问题,特别是我添加了一个 JButton 和一个 JLabel,但它们似乎并不尊重我给它们的大小和位置。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test extends JFrame{
private JPanel panel,panel2;
private JLabel insMatriz;
private JButton envMatriz;
public test(){
super("Test");
setLayout(new BorderLayout());
getContentPane().setBackground(Color.red);
panel = new JPanel();
add(panel,BorderLayout.WEST);
panel.setBackground(Color.blue);
panel2 = new JPanel();
add(panel2,BorderLayout.EAST);
panel2.setBackground(Color.GRAY);
panel.setPreferredSize(new Dimension(330,300));
panel2.setPreferredSize(new Dimension(330,300));
insMatriz = new JLabel();
panel.add(insMatriz);
insMatriz.setSize(new Dimension(50,10));
insMatriz.setLocation(5,5);
insMatriz.setText("Insert:");
envMatriz = new JButton();
panel2.add(envMatriz);
envMatriz.setSize(new Dimension(100,50));
envMatriz.setLocation(5,5);
envMatriz.setText("Submit");
setSize(700,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}