java panel和button,java – JButton在JPanel上,它不应该

这篇博客探讨了如何在Java Swing中使用GridLayout和CardLayout来创建复杂的用户界面。示例展示了如何在一个网格中嵌套另一个网格,并利用CardLayout在多个面板间切换。文章通过代码示例解释了如何设置布局管理器,以及如何添加和操作组件,以实现动态导航功能。
摘要由CSDN通过智能技术生成

而不是setLayout(null),学习使用layouts对您有利.下面的示例使用一系列nested layouts在另一个网格中添加一个网格.

zXKBC.png

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

/**

* @see https://stackoverflow.com/a/36243395/230513

*/

public class Test {

private static final int ROW = 2;

private static final int COL = 5;

private void display() {

JFrame f = new JFrame("Test");

f.setLayout(new GridLayout(0, 1));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel top = new JPanel(new GridBagLayout());

top.setBackground(Color.darkGray);

JLabel label = new JLabel("Post no bills.");

label.setForeground(Color.yellow);

top.add(label);

f.add(top);

f.add(createGridPanel());

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

private JPanel createGridPanel() {

JPanel p = new JPanel(new GridLayout(ROW, COL, 5, 5));

p.setBorder(BorderFactory.createLineBorder(Color.yellow,5));

p.setBackground(Color.yellow);

for (int r = 0; r < ROW; r++) {

for (int c = 0; c < COL; c++) {

p.add(createSubPanel());

}

}

return p;

}

private JPanel createSubPanel() {

JPanel p = new JPanel(new GridLayout(0, 1));

JPanel top = new JPanel();

top.add(new JButton("One"));

top.add(new JButton("Two"));

JPanel bot = new JPanel();

bot.add(new JRadioButton("A"));

bot.add(new JRadioButton("B"));

bot.add(new JRadioButton("C"));

bot.add(new JRadioButton("D"));

p.add(top);

p.add(bot);

return p;

}

public static void main(String[] args) {

EventQueue.invokeLater(new Test()::display);

}

}

附录:“我想……下一个和后面两个JButton.”

要允许按钮从一个面板导航到另一个面板,请使用CardLayout,如here所示,并在下面进行修订.

33e93d76b9520633a53caaf32b1c493c.png

import java.awt.BorderLayout;

import java.awt.CardLayout;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

/**

* @see https://stackoverflow.com/a/36243395/230513

*/

public class CardPanel extends JPanel {

private static final JPanel cards = new JPanel(new CardLayout());

private final String name;

public CardPanel(String name) {

super(new GridLayout(0, 1));

this.name = name;

JPanel top = new JPanel(new GridBagLayout());

top.setBackground(Color.darkGray);

JLabel label = new JLabel(name);

label.setForeground(Color.yellow);

top.add(label);

JPanel bot = new JPanel();

bot.setBorder(BorderFactory.createLineBorder(Color.yellow, 5));

bot.add(new JRadioButton("A"));

bot.add(new JRadioButton("B"));

bot.add(new JRadioButton("C"));

bot.add(new JRadioButton("D"));

this.add(top);

this.add(bot);

}

@Override

public String toString() {

return name;

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

create();

}

});

}

private static void create() {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

for (int i = 1; i < 9; i++) {

CardPanel p = new CardPanel("Panel " + String.valueOf(i));

cards.add(p, p.toString());

}

JPanel control = new JPanel();

control.add(new JButton(new AbstractAction("⊲Prev") {

@Override

public void actionPerformed(ActionEvent e) {

CardLayout cl = (CardLayout) cards.getLayout();

cl.previous(cards);

}

}));

control.add(new JButton(new AbstractAction("Next⊳") {

@Override

public void actionPerformed(ActionEvent e) {

CardLayout cl = (CardLayout) cards.getLayout();

cl.next(cards);

}

}));

f.add(cards, BorderLayout.CENTER);

f.add(control, BorderLayout.SOUTH);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用`getParent()`方法来获得一个组件的父级容器(例如 JPanel)。在你的 JButton 上调用此方法,它将返回包含该 JButtonJPanel。以下是一个示例代码片段: ```java JButton myButton = new JButton("Click Me"); JPanel parentPanel = (JPanel) myButton.getParent(); ``` 请注意,如果 JButton 不在任何 JPanel 中,则 getParent() 方法将返回 null。因此,在使用返回值之前,请确保检查是否为 null。 ### 回答2: 要想通过点击一个 JButton 来获取包含它的 JPanel,我们可以使用事件监听器来实现。首先,我们需要为 JButton 添加一个 ActionListener,然后在 ActionListener 的实现中获取 JButton 所在的 JPanel。 以下是一个简单的示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JButtonExample { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); JButton button = new JButton("点击我"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JPanel containingPanel = (JPanel) button.getParent(); JOptionPane.showMessageDialog(frame, "按钮所在的面板是:" + containingPanel.getName()); } }); panel.add(button); frame.add(panel); frame.setVisible(true); } } ``` 在这个示例中,我们创建了一个 JFrame,并添加了一个 JPanel 和一个 JButton。为了获取按钮所在的 JPanel,我们为 JButton 添加了一个 ActionListener,并在 ActionListener 中使用 `button.getParent()` 方法获取按钮的父容器,然后将其强制转换为 JPanel。 在点击按钮后,将弹出一个包含按钮所在的 JPanel 名称的消息对话框。 希望这个示例能够帮助你理解如何通过点击 JButton 获取包含它的 JPanel。 ### 回答3: 要获取包含特定JButtonJPanel,可以使用以下步骤: 1. 创建一个JButton对象:JButton button = new JButton("按钮"); 2. 创建一个JPanel对象:JPanel panel = new JPanel(); 3. 把JButton添加到JPanel上:panel.add(button); 4. 创建一个ActionListener监听器,用于处理按钮点击事件: ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { JButton clickedButton = (JButton) e.getSource(); // 获取被点击的按钮 Container parent = clickedButton.getParent(); // 获取按钮所在的容器 if (parent instanceof JPanel) { JPanel containingPanel = (JPanel) parent; // 在这里可以使用containingPanel来对包含该按钮的面板进行操作 } } }; 5. 将监听器添加到按钮上:button.addActionListener(listener); 在以上步骤中,我们首先创建一个JButton对象和一个JPanel对象。然后,将按钮添加到面板中。接下来,我们创建一个ActionListener监听器来处理按钮点击事件。在ActionListener中,我们根据点击的按钮获取它的父容器,如果父容器是一个JPanel,那么我们就可以对这个包含按钮的面板进行操作。最后,我们将监听器添加到按钮上。 通过这个方法,当点击按钮时,我们就可以使用containingPanel来操作包含该按钮的JPanel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值