我有2个JPanels和1个JFrame,当我单击一个按钮时,我试图在面板之间切换。我不想使用CardLayout,因为我想要不同的面板,并且对于CardLayouts我只能为两个面板使用相同的按钮。我的代码是:
package javaapplication2;
import javax.swing.JPanel;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
public NewJFrame() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(121, 183, 60));
setSize(200, 300);
setResizable(false);
/**panel1**/
jButton1 = new javax.swing.JButton();
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.setText("jButton1");
panel1.setBackground(new java.awt.Color(121, 183, 60));
panel1.setMaximumSize(new java.awt.Dimension(100, 200));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel1);
panel1.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(164, 164, 164)
.addComponent(jButton1)
.addContainerGap(172, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(jButton1)
.addContainerGap(158, Short.MAX_VALUE))
);
/**panel2**/
jButton2 = new javax.swing.JButton();
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jButton2.setText("jButton2");
panel2.setBackground(new java.awt.Color(101, 13, 61));
panel2.setMaximumSize(new java.awt.Dimension(100, 200));
javax.swing.GroupLayout layout2 = new javax.swing.GroupLayout(panel2);
panel2.setLayout(layout2);
layout2.setHorizontalGroup(
layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout2.createSequentialGroup()
.addGap(164, 164, 164)
.addComponent(jButton2)
.addContainerGap(172, Short.MAX_VALUE))
);
layout2.setVerticalGroup(
layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout2.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(jButton2)
.addContainerGap(158, Short.MAX_VALUE))
);
add(panel2);
pack();
}
public void changePanel(){
getContentPane().removeAll();
add(panel1);
invalidate();
repaint();
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
changePanel();
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
changePanel();
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
}
这是一个帮助您的小例子:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2(contentPane);
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel
{
private JButton jcomp4;
private JPanel contentPane;
public MyPanel(JPanel panel)
{
contentPane = panel;
setOpaque(true);
setBackground(Color.RED.darker().darker());
//construct components
jcomp4 = new JButton ("openNewWindow");
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
add(jcomp4);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
}
class MyPanel2 extends JPanel
{
private JButton jcomp1;
private JPanel contentPane;
public MyPanel2(JPanel panel)
{
contentPane = panel;
setOpaque(true);
setBackground(Color.GREEN.darker().darker());
//construct components
jcomp1 = new JButton ("Back");
jcomp1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
add(jcomp1);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
}
最新编辑
显示JPanel
您的选择,内部CardLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private MyPanel2 panel3;
private JComboBox choiceBox;
private String[] choices = {
"Panel 1",
"Panel 2",
"Panel 3"
};
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
choiceBox = new JComboBox(choices);
panel1 = new MyPanel(contentPane, this);
panel2 = new MyPanel2(contentPane
, Color.GREEN.darker().darker(), this);
panel3 = new MyPanel2(contentPane
, Color.DARK_GRAY, this);
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
contentPane.add(panel3, "Panel 3");
frame.getContentPane().add(choiceBox, BorderLayout.PAGE_START);
frame.getContentPane().add(contentPane, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JComboBox getChoiceBox()
{
return choiceBox;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel
{
private JButton jcomp4;
private JPanel contentPane;
private JComboBox choiceBox;
public MyPanel(JPanel panel, CardLayoutExample cle)
{
choiceBox = cle.getChoiceBox();
contentPane = panel;
setOpaque(true);
setBackground(Color.RED.darker().darker());
//construct components
jcomp4 = new JButton ("Show New Panel");
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String changeToPanel = (String) choiceBox.getSelectedItem();
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, changeToPanel);
}
});
add(jcomp4);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
}
class MyPanel2 extends JPanel
{
private JButton jcomp1;
private JPanel contentPane;
private Color backgroundColour;
private JComboBox choiceBox;
public MyPanel2(JPanel panel, Color c, CardLayoutExample cle)
{
contentPane = panel;
backgroundColour = c;
choiceBox = cle.getChoiceBox();
setOpaque(true);
setBackground(backgroundColour);
//construct components
jcomp1 = new JButton ("Show New Panel");
jcomp1.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String changeToPanel = (String) choiceBox.getSelectedItem();
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, changeToPanel);
}
});
add(jcomp1);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
}
我有一个问题,就是在一次单击一个按钮的过程中处理两个JFrames。我有2个JFrames(SUC_Forms和add_suc),当SUC_Forms中的按钮“Add Records”被点击时,将显示另一个新的JFrame,它是add_suc JFrame,包含注册/归档的文本框。当我按ok验证并插入数据库中的所有数据时,将弹出一个JDialogbox进行确认。当我点击“OK”后,两个JFrame
问题内容: 我试图在弹出新框架时禁用“主” 。我想要它,所以您不能在该框架上单击或拖动任何东西。我尝试将新框架设为a ,但这并没有禁用其他框架。我还查看了有关此的另一篇文章,建议将其设置为a,但仍然无法正常工作。我真的需要帮助。谢谢。这是我正在使用的代码,他们有什么问题吗? 代表“主”框架。 问题答案: 您已经设置了JDialog#setModal或JDialog#ModalityTypes,可能
问题内容: 我下棋。我写了3节课。如果是游戏则为第一。(棋盘,棋子等),另外一个用于菜单。(新,打开,设置时间等按钮) 他们两个都使用JFrame。 我想将上述两个类都放在第3类中。例如,“游戏”窗口在左侧,菜单在右侧。第三类还将通过JFrame显示整个应用程序。 怎么做? 问题答案: 您不能将一个JFrame放入另一个。您在这里有几个设计选择。您可以将JFrames更改为JPanels。这可能是
问题内容: 我想知道是否有一种方法,通过单击“ X”,让程序在关闭JFrame之前执行一些代码。该方法仅使用整数。 问题答案: 您可能对使用感兴趣。本教程。
我有一个带有登录名(JPanel)的JFrame,可以从菜单栏中选择。菜单栏还根据用户角色显示选项。当用户第一次登录创建的JFrame实例时,当我在菜单栏上选择login并在new JFrame上实例化时,我的问题就出现了,因为我显然打开了两个JFrame,我找不到从JPanel隐藏或关闭之前JFrame的方法。在实例化新JFrame或任何其他可能的解决方案之前,有没有办法隐藏/处理所有打开的窗口
我正在做一个实验2D物理的个人项目。现在,我正在尝试设置JFrame,以便它每秒更新60次。但是,当我调用方法开始更新JFrame时,我不能关闭JFrame。但是如果我省略了调用方法的部分,我可以关闭JFrame。这是主要代码: 这是Frame的代码: 如果我不在start()中调用updater(),我可以关闭JFrame。我知道我离开running=true,但它仍然应该关闭,因为这在我的旧计