gpt4 book ai didi

java - jpanel不显示内容

转载 作者:行者123 更新时间:2023-12-01 08:01:08 24 4
gpt4 key购买 nike

我正在尝试创建一个通用的“确定/取消”对话框,它可以接受添加两个抽象方法(void ok() 和 void cancel())的子类 JPanel;

这个想法是,当按下“确定”按钮或“取消”按钮时,将调用子类面板上的相应方法。但是,我无法让主框架显示 JPanel。

这是 OkCancelFrame 代码。如果运行,它应该显示一个确定/取消表单,其中包含文本“现在你看到我了”。

import javax.swing.JLabel;

public class OkCancelForm extends javax.swing.JFrame {

private OkCancelPanel okCancelPanel;

public OkCancelForm(OkCancelPanel okCancelPanel) {
this.okCancelPanel = okCancelPanel;
initComponents();
mainPanel.add(okCancelPanel);
mainPanel.revalidate();
mainPanel.repaint();
pack();
}

private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

mainPanel = new javax.swing.JPanel();
javax.swing.JPanel buttonPanel = new javax.swing.JPanel();
javax.swing.JButton ok = new javax.swing.JButton();
javax.swing.JButton cancel = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());

mainPanel.setLayout(null);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
getContentPane().add(mainPanel, gridBagConstraints);

buttonPanel.setLayout(new java.awt.GridBagLayout());

ok.setText("Ok");
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okActionPerformed(evt);
}
});
buttonPanel.add(ok, new java.awt.GridBagConstraints());

cancel.setText("Cancel");
cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelActionPerformed(evt);
}
});
buttonPanel.add(cancel, new java.awt.GridBagConstraints());

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
getContentPane().add(buttonPanel, gridBagConstraints);

pack();
}

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
okCancelPanel.cancel();
this.dispose();
}

private void okActionPerformed(java.awt.event.ActionEvent evt) {
okCancelPanel.ok();
this.dispose();
}

public static void main(String args[]) {
OkCancelPanel panel = new OkCancelPanel() {
public void ok() { }
public void cancel() { }
};

panel.add(new JLabel("Now you see me"));

new OkCancelForm(panel).setVisible(true);
}

private javax.swing.JPanel mainPanel;
}

最佳答案

不要使用空布局,因为它会在后面咬你。严重地。您正在使用容器将组件添加到空布局,并且在执行此操作时,如果您不设置组件的大小和位置,它将默认为 0,0 位置和 0, 0 大小。一种解决方案是通过 setBounds(...) 设置组件的边界,但您仍然使用非常不灵活的布局管理器,它基本上将您的 GUI 逼到了角落。只是不要使用它。

<小时/>

编辑
David Postill做了一个很好的评论,您似乎正在尝试重新创建自己的 JOptionPane,为什么不简单地使用已经可用的 JOptionPane 类。您可能不知道可以将自己的 JPanel(或任何 JComponent)填充到 JOptionPane 中。例如下面的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;

import javax.swing.*;

@SuppressWarnings("serial")
public class OkCancelForm2 extends JPanel {
private static final int PREF_W = 300;
private static final int PREF_H = PREF_W;

public OkCancelForm2() {
JLabel label = new JLabel("What the Heck?", JLabel.CENTER);
label.setFont(label.getFont().deriveFont(32f));
label.setForeground(Color.yellow);

setLayout(new GridBagLayout());
add(label);
}

// just for fun
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0, Color.red, 20, 20, Color.blue, true));
g2.fillOval(0, 0, 2 * getWidth(), 2 * getHeight());
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

private static void createAndShowGui() {
OkCancelForm2 mainPanel = new OkCancelForm2();

int selection = JOptionPane.showConfirmDialog(null, mainPanel,
"My OK/Cancel Pane", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);

if (selection == JOptionPane.OK_OPTION) {
System.out.println("OK selected");
} else if (selection == JOptionPane.CANCEL_OPTION) {
System.out.println("Cancel selected");
} else {
System.out.println("JOptionPane closed with no button selected");
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
<小时/>

有这些行:

  OkCancelForm2 mainPanel = new OkCancelForm2();

int selection = JOptionPane.showConfirmDialog(null, mainPanel,
"My OK/Cancel Pane", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);

这会将 JPanel 派生对象 mainPanel 填充到 JOptionPane 中,并生成此 JOptinPane:

enter image description here

关于java - jpanel不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25230257/

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