我想为单独的项目创建一个 JTabbedPane
,使用 JPanel
和 GridBaglayout
,但每次都是 的第二个选项卡>JTabbedPane
bug 我的文本字段对象...
这是我的动物登记类、扩展类和主类 问题出在第二个表的文本字段中。
代码,只有问题......
代码:
public class Cad_Animals extends Register_Screen{
JTextField number = new JTextField(10);
JTextField name = new JTextField(30);
JTabbedPane pane1 = new JTabbedPane(1);
JTabbedPane pane2 = new JTabbedPane(1);
JTextField race = new JTextField(10);
JTextField proprietario = new JTextField(10);
JPanel jl1 = new JPanel();
JPanel jl2 = new JPanel();
JTextField father = new JTextField(10);
JTextField mother = new JTextField(10);
public Cad_Animals(){
super("Animal Register");
jl1.setLayout(new GridBagLayout());
jl2.setLayout(new GridBagLayout());
addComponent(1, 1, 1, 1, number,jl1);
addComponent(2, 1, 1, 1, name,jl2);
addComponent(3, 1, 1, 1, proprietario,jl1);
addComponent(2, 1, 1, 1, race,jl2);
addComponent(6, 1, 1, 1, father,jl1);
addComponent(7, 1, 1, 1, mother,jl1);
//pane1.addTab("table1", jl2);
//pane2.addTab("table2", jl1);
//addComponent(8, 1, 6, 6, pane1,1);
// addComponent(8, 2, 3, 3, pane2,1);
//painels("new2",jl2);
//painels("new2", jl1);
pack();
}
}
.
public class Register_Screen extends JFrame {
JTabbedPane la = new JTabbedPane();
public Register_Screen(String titulo) {
super(titulo);
getContentPane().setLayout(new BorderLayout());
setVisible(true);
pack();
}
public void addComponent(int line, int column, int height, int width, JComponent component,JComponent compnum) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = line;
gbc.gridx = column;
gbc.gridheight = 1; //1
gbc.gridwidth = 1; //1
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(1, 5, 3, 5);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx++;
gbc.gridheight = height;
gbc.gridwidth = width;
//jpCampos.add(component, gbc);
compnum.add(component, gbc);
getContentPane().add(la);
la.addTab("West",compnum);
//jtpTabs.add("asdsa",jpCampos);
}
}
.
public class MainClass {
public static void main(String args[]) throws InterruptedException{
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Não foi possivel Carregar NimbusLookandFeel");
}
Cad_Animals cad = new Cad_Animals();
最佳答案
猜测:
您没有设置 GridBagConstraint 的weightx 或权重,因此您的 GridBagConstraints 的默认值为 0.0,这会将所有组件聚集到中心。考虑将您的weightx和weighty约束字段的值设置为1.0。
即,
gbc.gridy = line;
gbc.gridx = column;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(1, 5, 3, 5);
// ***** add these two lines
gbc.weightx = 1.0;
gbc.weighty = 1.0;
如果这个答案没有帮助,那么我坚持我在对您原始问题的评论中提出的建议,即您花一些时间创建并发布 minimal example program .
关于java - JTabbedPane、Jpanel 和 Gridbaglayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25611701/