我正在尝试创建一个通用的Ok / Cancel对话框,它可以接受添加了两个抽象方法的子类JPanel(void ok()和void cancel());
这个想法是当按下Ok按钮或Cancel按钮时,会调用子类面板上的相应方法。但是,我无法获得显示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 :(得分:6)
不要使用空布局,因为它会在后面咬你。认真。您正在使用容器向null布局添加组件,在执行此操作时,如果您未设置组件的大小和位置,则默认为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:
答案 1 :(得分:0)
看看这个。它正在发挥作用。
import javax.swing.JLabel;
import javax.swing.JPanel;
public class OkCancelForm extends javax.swing.JFrame {
private OkCancelPanel okCancelPanel;
abstract static class OkCancelPanel extends JPanel{
public abstract void ok();
public abstract void cancel();
}
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;
}