繁体   English   中英

JComboBox对JTextfield的影响

[英]Effect on JTextfield due to JComboBox

我正在使用JCombobox ,下面有一个包含JTextFields的面板。 每当我点击下拉列表(JCombobox)JTextField某些部分就会消失。 我不知道该怎么办。 我无法在这里发布问题的照片因为我是新用户而且我的声誉低于10。

public class MainClass {
public static void main(String args[]){
    Form form = new Form();
    int width = 400;
    int height = 400;
    form.setSize(width, height);
    form.setVisible(true);
    form.setTitle("Create Network");
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    form.setLocation((screenSize.width-width)/2, (screenSize.height-height)/2);
}
}

这是表单类

public class Form extends JFrame {

private JLabel ipAddress, networkTopo, numNodes;
private JTextField nodes;
private JButton addIp;
private JButton removeIp;
private JButton next, back;
private JPanel jPanel, jPanel1, jPanel2, commonPanel;
private JComboBox<String> dropDown;

private String[] topologies = { "Grid", "Diagnol Grid", "Bus", "Ring",
        "Star" };

public Form() {
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setResizable(false);
    this.getContentPane().setBackground(Color.WHITE);

    // add(Box.createRigidArea(new Dimension(0,10)));
    GridLayout commonPanelLayout = new GridLayout(0, 2);
    commonPanelLayout.setVgap(10);
    commonPanel = new JPanel(commonPanelLayout);
    commonPanel.setVisible(true);
    commonPanel.setAlignmentX(commonPanel.LEFT_ALIGNMENT);
    commonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    commonPanel.setBackground(Color.white);

    numNodes = new JLabel("Number of Nodes");
    commonPanel.add(numNodes);

    nodes = new JTextField(20);
    commonPanel.add(nodes);

    networkTopo = new JLabel("Network Topology");
    commonPanel.add(networkTopo);

    dropDown = new JComboBox<String>(topologies);
    dropDown.setBackground(Color.WHITE);
    commonPanel.add(dropDown);

    add(commonPanel);

    add(Box.createRigidArea(new Dimension(0, 10)));
    ipAddress = new JLabel("IP Addresses");
    ipAddress.setAlignmentX(ipAddress.LEFT_ALIGNMENT);
    ipAddress.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
    add(ipAddress);

    add(Box.createRigidArea(new Dimension(0, 10)));
    jPanel = new JPanel(new FlowLayout());
    jPanel.setVisible(true);
    jPanel.setAlignmentX(jPanel.LEFT_ALIGNMENT);
    jPanel.setBackground(Color.WHITE);
    // jPanel1.setAutoscrolls(true);
    add(jPanel);

    GridLayout layout = new GridLayout(0, 1);
    jPanel1 = new JPanel();
    layout.setVgap(10);
    // jPanel1.setBackground(Color.WHITE);
    jPanel1.setVisible(true);

    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(jPanel1);
    scroll.setAutoscrolls(true);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setPreferredSize(new Dimension(300, 150));
    jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
            Integer.MAX_VALUE));
    jPanel.add(scroll);

    jPanel2 = new JPanel(new GridLayout(0, 1, 10, 10));
    jPanel2.setBackground(Color.WHITE);
    jPanel2.setVisible(true);
    jPanel.add(jPanel2);

    addIp = new JButton("Add");
    jPanel2.add(addIp);

    removeIp = new JButton("Remove");
    jPanel2.add(removeIp);

    JPanel savePanel = new JPanel(new FlowLayout());
    savePanel.setVisible(true);
    savePanel.setAlignmentX(savePanel.LEFT_ALIGNMENT);
    savePanel.setBackground(Color.white);

    back = new JButton("Back");
    back.setAlignmentX(next.LEFT_ALIGNMENT);
    back.setEnabled(false);
    savePanel.add(back);

    next = new JButton("Next");
    next.setAlignmentX(next.LEFT_ALIGNMENT);
    savePanel.add(next);

    add(savePanel);

    AddIPEvent addIPEvent = new AddIPEvent();
    addIp.addActionListener(addIPEvent);

    RemoveIPEvent removeIPEvent = new RemoveIPEvent();
    removeIp.addActionListener(removeIPEvent);

}

public class AddIPEvent implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        JPanel subPanel = new JPanel(new FlowLayout());
        // subPanel.setBackground(Color.WHITE);
        subPanel.setVisible(true);
        JCheckBox jCheckBox = new JCheckBox();
        // jCheckBox.setBackground(Color.WHITE);
        subPanel.add(jCheckBox);

        JTextField ip = new JTextField(12);
        subPanel.add(ip);

        JTextField nodesInIp = new JTextField(6);
        nodesInIp.setVisible(false);
        subPanel.add(nodesInIp);

        jPanel1.add(subPanel);
        jPanel1.repaint();
        jPanel1.revalidate();
    }
}

public class RemoveIPEvent implements ActionListener {

    public void removeIP(Container container) {
        for (Component c : container.getComponents()) {
            if (c instanceof JCheckBox) {
                if (((JCheckBox) c).isSelected()) {
                    jPanel1.remove(container);
                    jPanel.revalidate();
                    jPanel1.repaint();
                }
            } else if (c instanceof Container) {
                removeIP((Container) c);
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        removeIP(jPanel1);
    }
}
}

单击Add按钮然后单击JComboBox将使JTextField一部分消失。 有人可以指出错误吗?

每当我点击下拉列表(JCombobox)时,JTextField的某些部分就会消失。

//jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width, Integer.MAX_VALUE));

不要设置组件的首选大小。 布局管理器将在添加/删除组件时确定首选大小。 然后将根据需要显示滚动条。

jPanel1.repaint();
jPanel1.revalidate();

订单应该是:

jPanel1.revalidate(); 
jPanel1.repaint(); 

revalidate()首先在重新绘制之前调用布局管理器。

//form.setSize(width, height);
form.pack();

同样,不要尝试设置大小。 让布局经理完成他们的工作。 pack()将根据添加到框架的组件的大小来确定框架的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM