gpt4 book ai didi

java - GUI 上的按钮有什么问题

转载 作者:行者123 更新时间:2023-12-01 04:28:27 25 4
gpt4 key购买 nike

我在此 GUI 的第三个面板上创建了一个“下一步”按钮。该面板有一个 JTree,上面有一个可折叠复选框,但是当我打开树时,按钮会消失,当我选中树上的框时,按钮会重新出现。我想阻止按钮消失,如果您执行代码并继续到带有树的第三个面板,您就会明白我在说什么,有什么想法可以解决这个问题吗?

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class MyWizard {

private JFrame frame = new JFrame("My Wizard");
private JPanel panelContainer = new JPanel();
private JPanel panelFirst = new JPanel();
private JPanel panelSecond = new JPanel();
private JPanel panelThird = new JPanel();
private JButton btNext = new JButton("Next");
private JButton btNextTwo = new JButton("Next");
private JButton btNextThree = new JButton("Next");
private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
private JCheckBox btSan, btNFS, btYUMserver;
private CardLayout c1 = new CardLayout();
private JScrollPane scrollPane;
private JLabel lblPicture, lblPictureTwo;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("FMW Components");

public MyWizard() {
// tree code
final DefaultMutableTreeNode accessibility = add(root, "DB Tier", true);
add(accessibility, "RAC", false);
add(accessibility, "Gateways", false);
add(accessibility, "Datavault", false);
add(accessibility, "Agent", false);
add(accessibility, "Custom Databases", false);
root.add(accessibility);

final DefaultMutableTreeNode browsing = new DefaultMutableTreeNode("APP Tier");
add(browsing, "IDM (OID, OVD)", false);
add(browsing, "IAM (Access Manager)", false);
add(browsing, "BIEE", false);
add(browsing, "Forms and Reports", false);
add(browsing, "Discoverer", false);
add(browsing, "Apps", false);
add(browsing, "Apex(4.2.1)", false);
root.add(browsing);

final DefaultTreeModel treeModel = new DefaultTreeModel(root);
final JTree tree = new JTree(treeModel);

final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
tree.setCellRenderer(renderer);

final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
tree.setCellEditor(editor);
tree.setEditable(true);
scrollPane = new JScrollPane(tree);
// tree code ends

panelFirst.setLayout(null);
panelSecond.setLayout(null);
panelThird.setLayout(new BorderLayout());

panelContainer.setLayout(c1);
panelFirst.add(btNext);
panelSecond.add(btNextTwo);
panelThird.add(scrollPane);
panelThird.add(btNextThree);

panelFirst.setBackground(Color.white);
panelSecond.setBackground(Color.white);

panelContainer.add(panelFirst, "1");
panelContainer.add(panelSecond, "2");
panelContainer.add(panelThird, "3");
c1.show(panelContainer, "1");
panelThird.add(scrollPane);

btNext.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
c1.show(panelContainer, "2");

}

});

btNextTwo.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
c1.show(panelContainer, "3");

}

});
RadioButtons();
Button();
Buttons();
CheckList();
groupButton();
ButtonsTwo();
Image();
frame.add(panelContainer);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setSize(400, 310);
frame.setVisible(true);
}

public void RadioButtons() {
btLdap = new JRadioButton("Ldap");
btLdap.setBounds(60, 85, 100, 20);
btLdap.setBackground(Color.white);
panelFirst.add(btLdap);

btKerbegos = new JRadioButton("Kerbegos");
btKerbegos.setBounds(60, 115, 100, 20);
btKerbegos.setBackground(Color.white);
panelFirst.add(btKerbegos);

btSpnego = new JRadioButton("Spnego");
btSpnego.setBounds(60, 145, 100, 20);
btSpnego.setBackground(Color.white);
panelFirst.add(btSpnego);

btSaml2 = new JRadioButton("Saml2");
btSaml2.setBounds(60, 175, 100, 20);
btSaml2.setBackground(Color.white);
panelFirst.add(btSaml2);
}

public void Button() {
btNext.setBounds(250, 240, 100, 20);
}

public void CheckList() {
btSan = new JCheckBox("San");
btSan.setBounds(60, 85, 100, 20);
btSan.setBackground(Color.white);
panelSecond.add(btSan);

btNFS = new JCheckBox("NFS");
btNFS.setBounds(60, 115, 100, 20);
btNFS.setBackground(Color.white);
panelSecond.add(btNFS);

btYUMserver = new JCheckBox("Spnego");
btYUMserver.setBounds(60, 145, 100, 20);
btYUMserver.setBackground(Color.white);
panelSecond.add(btYUMserver);
}

public void Buttons() {
btNextTwo.setBounds(250, 240, 100, 20);
}

public void ButtonsTwo() {
btNextThree.setBounds(250, 240, 100, 20);
}

public void Image() {
ImageIcon image = new ImageIcon("iconpic.png");
lblPicture = new JLabel(image);
lblPicture.setBounds(140, 5, 330, 270);
panelFirst.add(lblPicture);

ImageIcon imageTwo = new ImageIcon("iconpic.png");
lblPictureTwo = new JLabel(imageTwo);
lblPictureTwo.setBounds(140, 5, 330, 270);
panelSecond.add(lblPictureTwo);
}

private void groupButton() {

ButtonGroup bg1 = new ButtonGroup();

bg1.add(btLdap);
bg1.add(btKerbegos);
bg1.add(btSpnego);
bg1.add(btSaml2);

}

private static DefaultMutableTreeNode add(final DefaultMutableTreeNode parent, final String text,
final boolean checked) {
final CheckBoxNode data = new CheckBoxNode(text, checked);
final DefaultMutableTreeNode node = new DefaultMutableTreeNode(data);
parent.add(node);
return node;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new MyWizard();
}
});

}
}

最佳答案

您正在将 panelThird 中的组件添加到同一位置。

panelThird.add(scrollPane);
panelThird.add(btNextThree);

将其更改为例如

panelThird.add(scrollPane, BorderLayout.CENTER);
panelThird.add(btNextThree, BorderLayout.SOUTH);

此外,删除几行后向 panelThird 添加的第二个 scrollPane 内容。实际上,使用 null 布局会使任务变得比应有的困难。 Swing 被设计为与布局管理器一起使用,并尝试这样做,否则您将一直与框架作斗争。 (除了 GUI 不能很好地处理调整大小、翻译、不同主题……)

关于java - GUI 上的按钮有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229398/

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