使用另一个 jPanel 上的按钮修改 jPanel
Modifying a jPanel using buttons on another jPanel
我刚刚接触 Java,我正在为我的大学 class 做一个项目。
我正在开发一款百万富翁游戏,但遇到了困难。
我有一个 JFrame class,其中有 2 个面板。第一个由按钮组成,第二个是我想通过按下按钮来更改的面板。按钮有自己的 class 及其构造函数,面板也是如此,因为它们具有不同的布局。我需要在按钮 class 中创建一个方法以从框架中移除第二个面板并添加第三个面板(在另一个 more JPanel class 中有描述)。所以我在技术上需要从按钮 class 方法访问我的 JFrame class 构造函数。有办法吗?
我有我的第一个面板 class 和我的按钮 class 及其 ClickListener 方法。
现在我需要知道如何修改 Button 方法中的 JFrame class 以在单击时关闭第一个面板,在相同位置打开另一个面板。
按钮方法
public class ClickListenerD1 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
buttonPressed();
}
private void buttonPressed()
{
JPanel panel3 = new Domanda1();
}
}
主 JFrame class
package nuovaPartita;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Visualizza la finestra di gioco.
*/
public class NuovaPartitaViewer extends JFrame
{
private static final int FRAME_LUNGH = 1600;
private static final int FRAME_ALT = 900;
JPanel panel1 = new NuovaPartitaComp1();
JPanel panel2 = new Start();
/**
* Costruisce una finestra di gioco su cui vengono visualizzati due
pannelli.
*/
public NuovaPartitaViewer()
{
setSize(FRAME_LUNGH, FRAME_ALT);
setTitle("CHI VUOL ESSER MILIONARIO?");
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);
getContentPane().setBackground(Color.BLACK);
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
}
}
谢谢
您可以只在按钮的动作侦听器中设置按钮对应的面板。
public class NuovaPartitaViewer extends JFrame {
public NuovaPartitaViewer() {
JPanel p1 = new JPanel();
p1.add(new JLabel("Panel 1"));
JPanel p2 = new JPanel();
p2.add(new JLabel("Panel 2"));
JPanel p3 = new JPanel();
p3.add(new JLabel("Panel 3"));
JPanel p4 = new JPanel();
p4.add(new JLabel("Panel 4"));
JButton b1 = new JButton("Button 1");
b1.addActionListener(e -> setPanel(p1));
JButton b2 = new JButton("Button 2");
b2.addActionListener(e -> setPanel(p2));
JButton b3 = new JButton("Button 3");
b3.addActionListener(e -> setPanel(p3));
JButton b4 = new JButton("Button 4");
b4.addActionListener(e -> setPanel(p4));
JPanel buttonsPanel = new JPanel(new GridLayout(2, 2));
buttonsPanel.add(b1);
buttonsPanel.add(b2);
buttonsPanel.add(b3);
buttonsPanel.add(b4);
BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);
getContentPane().setBackground(Color.BLACK);
add(buttonsPanel, BorderLayout.WEST);
add(p1, BorderLayout.CENTER);
setTitle("CHI VUOL ESSER MILIONARIO?");
pack();
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private void setPanel(JPanel p) {
add(p, BorderLayout.CENTER);
revalidate();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new NuovaPartitaViewer());
}
}
如果您的所有按钮都是自定义的 (extends JButton
),那么您可以直接在 class 中添加动作侦听器代码。在构造函数中传入相应的JPanel
,这样就可以在动作监听器中使用了。
此外:
- 不要设置框架的大小,请改用
pack()
。
- 调用
setVisible(true)
作为方法中的最后一件事。
我刚刚接触 Java,我正在为我的大学 class 做一个项目。 我正在开发一款百万富翁游戏,但遇到了困难。
我有一个 JFrame class,其中有 2 个面板。第一个由按钮组成,第二个是我想通过按下按钮来更改的面板。按钮有自己的 class 及其构造函数,面板也是如此,因为它们具有不同的布局。我需要在按钮 class 中创建一个方法以从框架中移除第二个面板并添加第三个面板(在另一个 more JPanel class 中有描述)。所以我在技术上需要从按钮 class 方法访问我的 JFrame class 构造函数。有办法吗?
我有我的第一个面板 class 和我的按钮 class 及其 ClickListener 方法。 现在我需要知道如何修改 Button 方法中的 JFrame class 以在单击时关闭第一个面板,在相同位置打开另一个面板。
按钮方法
public class ClickListenerD1 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
buttonPressed();
}
private void buttonPressed()
{
JPanel panel3 = new Domanda1();
}
}
主 JFrame class
package nuovaPartita;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Visualizza la finestra di gioco.
*/
public class NuovaPartitaViewer extends JFrame
{
private static final int FRAME_LUNGH = 1600;
private static final int FRAME_ALT = 900;
JPanel panel1 = new NuovaPartitaComp1();
JPanel panel2 = new Start();
/**
* Costruisce una finestra di gioco su cui vengono visualizzati due
pannelli.
*/
public NuovaPartitaViewer()
{
setSize(FRAME_LUNGH, FRAME_ALT);
setTitle("CHI VUOL ESSER MILIONARIO?");
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);
getContentPane().setBackground(Color.BLACK);
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
}
}
谢谢
您可以只在按钮的动作侦听器中设置按钮对应的面板。
public class NuovaPartitaViewer extends JFrame {
public NuovaPartitaViewer() {
JPanel p1 = new JPanel();
p1.add(new JLabel("Panel 1"));
JPanel p2 = new JPanel();
p2.add(new JLabel("Panel 2"));
JPanel p3 = new JPanel();
p3.add(new JLabel("Panel 3"));
JPanel p4 = new JPanel();
p4.add(new JLabel("Panel 4"));
JButton b1 = new JButton("Button 1");
b1.addActionListener(e -> setPanel(p1));
JButton b2 = new JButton("Button 2");
b2.addActionListener(e -> setPanel(p2));
JButton b3 = new JButton("Button 3");
b3.addActionListener(e -> setPanel(p3));
JButton b4 = new JButton("Button 4");
b4.addActionListener(e -> setPanel(p4));
JPanel buttonsPanel = new JPanel(new GridLayout(2, 2));
buttonsPanel.add(b1);
buttonsPanel.add(b2);
buttonsPanel.add(b3);
buttonsPanel.add(b4);
BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);
getContentPane().setBackground(Color.BLACK);
add(buttonsPanel, BorderLayout.WEST);
add(p1, BorderLayout.CENTER);
setTitle("CHI VUOL ESSER MILIONARIO?");
pack();
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private void setPanel(JPanel p) {
add(p, BorderLayout.CENTER);
revalidate();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new NuovaPartitaViewer());
}
}
如果您的所有按钮都是自定义的 (extends JButton
),那么您可以直接在 class 中添加动作侦听器代码。在构造函数中传入相应的JPanel
,这样就可以在动作监听器中使用了。
此外:
- 不要设置框架的大小,请改用
pack()
。 - 调用
setVisible(true)
作为方法中的最后一件事。