gpt4 book ai didi

java - JTabbedPane、Jpanel 和 Gridbaglayout

转载 作者:行者123 更新时间:2023-11-30 09:03:27 24 4
gpt4 key购买 nike

我想为单独的项目创建一个 JTabbedPane,使用 JPanelGridBaglayout,但是每次, 的第二个选项卡>JTabbedPane 错误我的文本字段对象...

那是我的动物注册类,扩展类和主类问题出在第二个表的文本字段中。

代码,只有问题...

代码:

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 也没有设置 weighty,因此您的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com