gpt4 book ai didi

java - 单击按钮后链接到其他面板

转载 作者:行者123 更新时间:2023-11-29 08:13:48 25 4
gpt4 key购买 nike

现在我有 3 个 Java 文件,它们是 .我在 TrainAndCOnductor 上有 2 个按钮。我的目的是,当我点击第一个按钮时,它将打开我的 The Conductor 面板。

public class JavaApplication1 {

public static void main(String[] args) {
TheConductor form = new TheConductor();
form.setVisible(true);
}
}
//Train And Conductor
public class TrainAndConductor extends javax.swing.JFrame {

/** Creates new form TrainAndConductor */
public TrainAndConductor() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Train Driver");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Conductor");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
add(TheConductor);
TheConductor.setVisible(true);

//The Conductor
public class TheConductor extends javax.swing.JFrame {

/** Creates new form TheConductor */
public TheConductor() {
initComponents();
}

最佳答案

这取决于您的布局是如何组织的。您可以使用 JTabbedPane 或 CardLayout实现这一目标。

关于java - 单击按钮后链接到其他面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067913/

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