Java JPanel 未显示
我不确定我做错了什么,但我的 JPanels 文本没有显示。我只收到问题编号文本,但问题没有显示。有什么想法我做错了吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class NewFrame extends JFrame {
JPanel centerpanel; // For the questions.
CardLayout card; // For the centerpanel.
JTextField tf; // Used in question 1.
boolean // Store selections for Q2.
q2Option1,
q2Option2,
q2Option3,
q2Option4;
JList q4List; // For question 4.
double // Score on each question.
q1Score = 0,
q2Score = 0,
q3Score = 0,
q4Score = 0;
// Constructor.
public NewFrame (int width, int height)
{
this.setTitle ("Snoot Club Membership Test");
this.setResizable (true);
this.setSize (width, height);
Container cPane = this.getContentPane();
// cPane.setLayout (new BorderLayout());
// First, a welcome message, as a Label.
JLabel L = new JLabel ("<html><b>Are you elitist enough for our exclusive club?"
+ " <br>Fill out the form and find out</b></html>");
L.setForeground (Color.blue);
cPane.add (L, BorderLayout.NORTH);
// Now the center panel with the questions.
card = new CardLayout ();
centerpanel = new JPanel ();
centerpanel.setLayout (card);
centerpanel.setOpaque (false);
// Each question will be created in a separate method.
// The cardlayout requires a label as second parameter.
centerpanel.add (firstQuestion (), "1");
centerpanel.add (secondQuestion(), "2");
centerpanel.add (thirdQuestion(), "3");
centerpanel.add (fourthQuestion(), "4");
cPane.add (centerpanel, BorderLayout.CENTER);
// Next, a panel of four buttons at the bottom.
// The four buttons: quit, submit, next-question, previous-question.
JPanel bottomPanel = getBottomPanel ();
cPane.add (bottomPanel, BorderLayout.SOUTH);
// Finally, show the frame.
this.setVisible (true);
}
// No-parameter constructor.
public NewFrame ()
{
this (500, 300);
}
// The first question uses labels for the question and
// gets input via a textfield. A panel containing all
// these things is returned. The question asks for
// a vacation destination: the more exotic the location,
// the higher the score.
JPanel firstQuestion ()
{
// We will package everything into a panel and return the panel.
JPanel subpanel = new JPanel ();
// We will place things in a single column, so
// a GridLayout with one column is appropriate.
subpanel.setLayout (new GridLayout (8,1));
JLabel L1 = new JLabel ("Question 1:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" Select a vacation destination");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
JLabel L3 = new JLabel (" 1. Baltimore");
L3.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L3);
JLabel L4 = new JLabel (" 2. Disneyland");
L4.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L4);
JLabel L5 = new JLabel (" 3. Grand Canyon");
L5.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L5);
JLabel L6 = new JLabel (" 4. French Riviera");
L6.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L6);
JLabel L7 = new JLabel ("Enter 1,2,3 or 4 below:");
L7.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L7);
// Here's the textfield to get user-input.
tf = new JTextField ();
tf.addActionListener (
new ActionListener () {
// This interface has only one method.
public void actionPerformed (ActionEvent a)
{
String q1String = a.getActionCommand();
if (q1String.equals ("2"))
q1Score = 2;
else if (q1String.equals ("3"))
q1Score = 3;
else if (q1String.equals ("4"))
q1Score = 4;
else
q1Score = 1;
}
}
);
subpanel.add (tf);
return subpanel;
}
// For the second question, a collection of checkboxes
// will be used. More than one selection can be made.
// A listener is required for each checkbox. The state
// of each checkbox is recorded.
JPanel secondQuestion ()
{
JPanel subpanel = new JPanel ();
subpanel.setLayout (new GridLayout (7,1));
JLabel L1 = new JLabel ("Question 2:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" Select ONE OR MORE things that ");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
JLabel L3 = new JLabel (" you put into your lunch sandwich");
L3.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L3);
// Initialize the selections to false.
q2Option1 = q2Option2 = q2Option3 = q2Option4 = false;
// First checkbox.
JCheckBox c1 = new JCheckBox ("Ham, beef or turkey");
c1.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option1 = c.isSelected();
}
}
);
subpanel.add (c1);
// Second checkbox.
JCheckBox c2 = new JCheckBox ("Cheese");
c2.addItemListener (
new ItemListener () {
// This is where we will react to a change in checkbox.
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option2 = c.isSelected();
}
}
);
subpanel.add (c2);
// Third checkbox.
JCheckBox c3 = new JCheckBox ("Sun-dried Arugula leaves");
c3.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option3 = c.isSelected();
}
}
);
subpanel.add (c3);
// Fourth checkbox.
JCheckBox c4 = new JCheckBox ("Lemon-enhanced smoked Siberian caviar");
c4.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option4 = c.isSelected();
}
}
);
subpanel.add (c4);
return subpanel;
}
// The third question allows only one among four choices
// to be selected. We will use radio buttons.
JPanel thirdQuestion ()
{
JPanel subpanel = new JPanel ();
subpanel.setLayout (new GridLayout (6,1));
JLabel L1 = new JLabel ("Question 3:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" And which mustard do you use?");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
// First, create the ButtonGroup instance.
// We will add radio buttons to this group.
ButtonGroup bGroup = new ButtonGroup();
// First checkbox.
JRadioButton r1 = new JRadioButton ("Who cares?");
r1.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 1;
}
}
);
bGroup.add (r1);
subpanel.add (r1);
// Second checkbox.
JRadioButton r2 = new JRadioButton ("Safeway Brand");
r2.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 2;
}
}
);
bGroup.add (r2);
subpanel.add (r2);
// Third checkbox.
JRadioButton r3 = new JRadioButton ("Fleishman's");
r3.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 3;
}
}
);
bGroup.add (r3);
subpanel.add (r3);
// Fourth checkbox.
JRadioButton r4 = new JRadioButton ("Grey Poupon");
r4.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 4;
}
}
);
bGroup.add (r4);
subpanel.add (r4);
return subpanel;
}
// For the fourth question we will use a drop-down Choice.
JPanel fourthQuestion ()
{
JPanel subpanel = new JPanel ();
subpanel.setLayout (new GridLayout (3,1));
JLabel L1 = new JLabel ("Question 4:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" Your movie preference, among these:");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
// Create a JList with options.
String[] movies = { "Lethal Weapon IV", "Titanic", "Saving Private Ryan",
"Le Art Movie avec subtitles"};
q4List = new JList (movies);
q4Score = 1;
q4List.addListSelectionListener (
new ListSelectionListener () {
public void valueChanged (ListSelectionEvent e)
{
q4Score = 1 + q4List.getSelectedIndex();
}
}
);
subpanel.add (q4List);
return subpanel;
}
void computeResult ()
{
// Clear the center panel.
centerpanel.removeAll();
// Create a new panel to display in the center.
JPanel subpanel = new JPanel (new GridLayout (5,1));
// Score on question 1.
JLabel L1 = new JLabel ("Score on question 1: " + q1Score);
L1.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L1);
// Score on question 2.
if (q2Option1) q2Score += 1;
if (q2Option2) q2Score += 2;
if (q2Option3) q2Score += 3;
if (q2Option4) q2Score += 4;
q2Score = 0.6 * q2Score;
JLabel L2 = new JLabel ("Score on question 2: " + q2Score);
L2.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L2);
// Score on question 3.
JLabel L3 = new JLabel ("Score on question 3: " + q3Score);
L3.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L3);
// Score on question 4.
JLabel L4 = new JLabel ("Score on question 4: " + q4Score);
L4.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L4);
// Weighted score.
double avg = (q1Score + q2Score + q3Score + q4Score) / (double) 4;
JLabel L5;
if (avg <= 3.5)
L5 = new JLabel ("Your average score: " + avg + " - REJECTED!");
else
L5 = new JLabel ("Your average score: " + avg + " - WELCOME!");
L5.setFont (new Font ("Serif", Font.BOLD, 20));
//L5.setAlignment (JLabel.CENTER);
subpanel.add (L5);
// Now add the new subpanel.
centerpanel.add (subpanel, "5");
// Need to mark the centerpanel as "altered"
centerpanel.invalidate();
// Everything "invalid" (e.g., the centerpanel above)
// is now re-computed.
this.validate();
}
JPanel getBottomPanel ()
{
// Create a panel into which we will place buttons.
JPanel bottomPanel = new JPanel ();
// A "previous-question" button.
JButton backward = new JButton ("Previous question");
backward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
backward.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
// Go back in the card layout.
card.previous (centerpanel);
}
}
);
bottomPanel.add (backward);
// A forward button.
JButton forward = new JButton ("Next question");
forward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
forward.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
// Go forward in the card layout.
card.next (centerpanel);
}
}
);
bottomPanel.add (forward);
// A submit button.
JButton submit = new JButton ("Submit");
submit.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
submit.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
// Perform submit task.
computeResult();
}
}
);
bottomPanel.add (submit);
JButton quitb = new JButton ("Quit");
quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
quitb.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
System.exit (0);
}
}
);
bottomPanel.add (quitb);
return bottomPanel;
}
}
public class Survey {
public static void main (String[] argv)
{
NewFrame nf = new NewFrame (600, 300);
}
}
I'm not sure what I am doing wrong, but the text for my JPanels is not showing up. I just get the question number text, but the question is not showing up. Any ideas what I am doing wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class NewFrame extends JFrame {
JPanel centerpanel; // For the questions.
CardLayout card; // For the centerpanel.
JTextField tf; // Used in question 1.
boolean // Store selections for Q2.
q2Option1,
q2Option2,
q2Option3,
q2Option4;
JList q4List; // For question 4.
double // Score on each question.
q1Score = 0,
q2Score = 0,
q3Score = 0,
q4Score = 0;
// Constructor.
public NewFrame (int width, int height)
{
this.setTitle ("Snoot Club Membership Test");
this.setResizable (true);
this.setSize (width, height);
Container cPane = this.getContentPane();
// cPane.setLayout (new BorderLayout());
// First, a welcome message, as a Label.
JLabel L = new JLabel ("<html><b>Are you elitist enough for our exclusive club?"
+ " <br>Fill out the form and find out</b></html>");
L.setForeground (Color.blue);
cPane.add (L, BorderLayout.NORTH);
// Now the center panel with the questions.
card = new CardLayout ();
centerpanel = new JPanel ();
centerpanel.setLayout (card);
centerpanel.setOpaque (false);
// Each question will be created in a separate method.
// The cardlayout requires a label as second parameter.
centerpanel.add (firstQuestion (), "1");
centerpanel.add (secondQuestion(), "2");
centerpanel.add (thirdQuestion(), "3");
centerpanel.add (fourthQuestion(), "4");
cPane.add (centerpanel, BorderLayout.CENTER);
// Next, a panel of four buttons at the bottom.
// The four buttons: quit, submit, next-question, previous-question.
JPanel bottomPanel = getBottomPanel ();
cPane.add (bottomPanel, BorderLayout.SOUTH);
// Finally, show the frame.
this.setVisible (true);
}
// No-parameter constructor.
public NewFrame ()
{
this (500, 300);
}
// The first question uses labels for the question and
// gets input via a textfield. A panel containing all
// these things is returned. The question asks for
// a vacation destination: the more exotic the location,
// the higher the score.
JPanel firstQuestion ()
{
// We will package everything into a panel and return the panel.
JPanel subpanel = new JPanel ();
// We will place things in a single column, so
// a GridLayout with one column is appropriate.
subpanel.setLayout (new GridLayout (8,1));
JLabel L1 = new JLabel ("Question 1:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" Select a vacation destination");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
JLabel L3 = new JLabel (" 1. Baltimore");
L3.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L3);
JLabel L4 = new JLabel (" 2. Disneyland");
L4.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L4);
JLabel L5 = new JLabel (" 3. Grand Canyon");
L5.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L5);
JLabel L6 = new JLabel (" 4. French Riviera");
L6.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L6);
JLabel L7 = new JLabel ("Enter 1,2,3 or 4 below:");
L7.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L7);
// Here's the textfield to get user-input.
tf = new JTextField ();
tf.addActionListener (
new ActionListener () {
// This interface has only one method.
public void actionPerformed (ActionEvent a)
{
String q1String = a.getActionCommand();
if (q1String.equals ("2"))
q1Score = 2;
else if (q1String.equals ("3"))
q1Score = 3;
else if (q1String.equals ("4"))
q1Score = 4;
else
q1Score = 1;
}
}
);
subpanel.add (tf);
return subpanel;
}
// For the second question, a collection of checkboxes
// will be used. More than one selection can be made.
// A listener is required for each checkbox. The state
// of each checkbox is recorded.
JPanel secondQuestion ()
{
JPanel subpanel = new JPanel ();
subpanel.setLayout (new GridLayout (7,1));
JLabel L1 = new JLabel ("Question 2:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" Select ONE OR MORE things that ");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
JLabel L3 = new JLabel (" you put into your lunch sandwich");
L3.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L3);
// Initialize the selections to false.
q2Option1 = q2Option2 = q2Option3 = q2Option4 = false;
// First checkbox.
JCheckBox c1 = new JCheckBox ("Ham, beef or turkey");
c1.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option1 = c.isSelected();
}
}
);
subpanel.add (c1);
// Second checkbox.
JCheckBox c2 = new JCheckBox ("Cheese");
c2.addItemListener (
new ItemListener () {
// This is where we will react to a change in checkbox.
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option2 = c.isSelected();
}
}
);
subpanel.add (c2);
// Third checkbox.
JCheckBox c3 = new JCheckBox ("Sun-dried Arugula leaves");
c3.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option3 = c.isSelected();
}
}
);
subpanel.add (c3);
// Fourth checkbox.
JCheckBox c4 = new JCheckBox ("Lemon-enhanced smoked Siberian caviar");
c4.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JCheckBox c = (JCheckBox) i.getSource();
q2Option4 = c.isSelected();
}
}
);
subpanel.add (c4);
return subpanel;
}
// The third question allows only one among four choices
// to be selected. We will use radio buttons.
JPanel thirdQuestion ()
{
JPanel subpanel = new JPanel ();
subpanel.setLayout (new GridLayout (6,1));
JLabel L1 = new JLabel ("Question 3:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" And which mustard do you use?");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
// First, create the ButtonGroup instance.
// We will add radio buttons to this group.
ButtonGroup bGroup = new ButtonGroup();
// First checkbox.
JRadioButton r1 = new JRadioButton ("Who cares?");
r1.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 1;
}
}
);
bGroup.add (r1);
subpanel.add (r1);
// Second checkbox.
JRadioButton r2 = new JRadioButton ("Safeway Brand");
r2.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 2;
}
}
);
bGroup.add (r2);
subpanel.add (r2);
// Third checkbox.
JRadioButton r3 = new JRadioButton ("Fleishman's");
r3.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 3;
}
}
);
bGroup.add (r3);
subpanel.add (r3);
// Fourth checkbox.
JRadioButton r4 = new JRadioButton ("Grey Poupon");
r4.addItemListener (
new ItemListener () {
public void itemStateChanged (ItemEvent i)
{
JRadioButton r = (JRadioButton) i.getSource();
if (r.isSelected()) q3Score = 4;
}
}
);
bGroup.add (r4);
subpanel.add (r4);
return subpanel;
}
// For the fourth question we will use a drop-down Choice.
JPanel fourthQuestion ()
{
JPanel subpanel = new JPanel ();
subpanel.setLayout (new GridLayout (3,1));
JLabel L1 = new JLabel ("Question 4:");
L1.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L1);
JLabel L2 = new JLabel (" Your movie preference, among these:");
L2.setFont (new Font ("SansSerif", Font.ITALIC, 15));
subpanel.add (L2);
// Create a JList with options.
String[] movies = { "Lethal Weapon IV", "Titanic", "Saving Private Ryan",
"Le Art Movie avec subtitles"};
q4List = new JList (movies);
q4Score = 1;
q4List.addListSelectionListener (
new ListSelectionListener () {
public void valueChanged (ListSelectionEvent e)
{
q4Score = 1 + q4List.getSelectedIndex();
}
}
);
subpanel.add (q4List);
return subpanel;
}
void computeResult ()
{
// Clear the center panel.
centerpanel.removeAll();
// Create a new panel to display in the center.
JPanel subpanel = new JPanel (new GridLayout (5,1));
// Score on question 1.
JLabel L1 = new JLabel ("Score on question 1: " + q1Score);
L1.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L1);
// Score on question 2.
if (q2Option1) q2Score += 1;
if (q2Option2) q2Score += 2;
if (q2Option3) q2Score += 3;
if (q2Option4) q2Score += 4;
q2Score = 0.6 * q2Score;
JLabel L2 = new JLabel ("Score on question 2: " + q2Score);
L2.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L2);
// Score on question 3.
JLabel L3 = new JLabel ("Score on question 3: " + q3Score);
L3.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L3);
// Score on question 4.
JLabel L4 = new JLabel ("Score on question 4: " + q4Score);
L4.setFont (new Font ("Serif", Font.ITALIC, 15));
subpanel.add (L4);
// Weighted score.
double avg = (q1Score + q2Score + q3Score + q4Score) / (double) 4;
JLabel L5;
if (avg <= 3.5)
L5 = new JLabel ("Your average score: " + avg + " - REJECTED!");
else
L5 = new JLabel ("Your average score: " + avg + " - WELCOME!");
L5.setFont (new Font ("Serif", Font.BOLD, 20));
//L5.setAlignment (JLabel.CENTER);
subpanel.add (L5);
// Now add the new subpanel.
centerpanel.add (subpanel, "5");
// Need to mark the centerpanel as "altered"
centerpanel.invalidate();
// Everything "invalid" (e.g., the centerpanel above)
// is now re-computed.
this.validate();
}
JPanel getBottomPanel ()
{
// Create a panel into which we will place buttons.
JPanel bottomPanel = new JPanel ();
// A "previous-question" button.
JButton backward = new JButton ("Previous question");
backward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
backward.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
// Go back in the card layout.
card.previous (centerpanel);
}
}
);
bottomPanel.add (backward);
// A forward button.
JButton forward = new JButton ("Next question");
forward.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
forward.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
// Go forward in the card layout.
card.next (centerpanel);
}
}
);
bottomPanel.add (forward);
// A submit button.
JButton submit = new JButton ("Submit");
submit.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
submit.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
// Perform submit task.
computeResult();
}
}
);
bottomPanel.add (submit);
JButton quitb = new JButton ("Quit");
quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15));
quitb.addActionListener (
new ActionListener () {
public void actionPerformed (ActionEvent a)
{
System.exit (0);
}
}
);
bottomPanel.add (quitb);
return bottomPanel;
}
}
public class Survey {
public static void main (String[] argv)
{
NewFrame nf = new NewFrame (600, 300);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚尝试过,它似乎有效 - 您使用的是哪个版本的 Java,在什么平台上?
顺便说一句:当你说它
实际做的是创建一个始终有 8 行的 GridLayout。如果您向其中添加另一个项目,它实际上会创建一个新列(GridLayout 只关心您传递给它的第一个非零参数)。
你真正的意思是:
I just tried it and it seems to work - what version of Java are you using, and on what platform?
BTW: When you say
what it actually does is create a GridLayout that will always have 8 rows. If you add another item to it, it will actually create a new column (GridLayout only cares about the first non-zero parameter you pass it).
You really mean:
我在您的代码中看到两件事:
CardLayout
中。您应该使用addLayoutComponent(Component, Object)
添加它们,并使用String
作为将用于显示面板的第二个参数。要显示面板,您应该调用show(Container, String)
方法,其中面板使用 CardLayout 作为第一个参数。Two things I see in your code:
CardLayout
. You should add them usingaddLayoutComponent(Component, Object)
, using aString
for the second arg that you will use to show the panel. To show your panel, you should then call theshow(Container, String)
method, with your panel that uses the CardLayout as the first arg.main
thread. You should do all GUI painting work in the EventDispatchThread. See this link for concepts around threading in Swing.