怎么样用java 点一次单选按钮,下面就显示出对应的信息。我做了一半了,就是显示不出对应信息,求助啊!!
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassEventDemo1exte...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo1 extends JApplet implements ActionListener
{
String university[]={"Card 1","Card 2","Card 3",
"Card 4"};
JRadioButton r[]=new JRadioButton[4];
public void init()
{
Container pane=getContentPane();
pane.setLayout(new BorderLayout());
p1=new JPanel();
ButtonGroup radioGroup=new ButtonGroup();
for(int i=0;i<4;i++)
{
r[i]=new JRadioButton(university[i]);
radioGroup.add(r[i]);
p1.add(r[i]);
}
pane.add("North",p1);
JPanel card1=new JPanel();
if (r[0].isSelected() )
{
card1.add(new JLabel("This is the first card"));
}
JPanel card2=new JPanel();
if (r[1].isSelected() )
{
card2.add(new JLabel("This is the second card"));
}
JPanel card3=new JPanel();
if (r[2].isSelected() )
{
card3.add(new JLabel("This is the third card"));
}
JPanel card4=new JPanel();
if (r[1].isSelected() )
{
card4.add(new JLabel("This is the forth card"));
}
p2=new JPanel();
layout=new CardLayout();
p2.setLayout(layout);
p2.add("Card 1",card1);
p2.add("Card 2",card2);
p2.add("Card 3",card3);
p2.add("Card 4",card4);
pane.add("Center",p2);
}
public void actionPerformed(ActionEvent e)
{
layout.show(p2,(String) e.getActionCommand());
}
private JPanel p1,p2;
private CardLayout layout;
} 展开
import java.awt.event.*;
import javax.swing.*;
public class EventDemo1 extends JApplet implements ActionListener
{
String university[]={"Card 1","Card 2","Card 3",
"Card 4"};
JRadioButton r[]=new JRadioButton[4];
public void init()
{
Container pane=getContentPane();
pane.setLayout(new BorderLayout());
p1=new JPanel();
ButtonGroup radioGroup=new ButtonGroup();
for(int i=0;i<4;i++)
{
r[i]=new JRadioButton(university[i]);
radioGroup.add(r[i]);
p1.add(r[i]);
}
pane.add("North",p1);
JPanel card1=new JPanel();
if (r[0].isSelected() )
{
card1.add(new JLabel("This is the first card"));
}
JPanel card2=new JPanel();
if (r[1].isSelected() )
{
card2.add(new JLabel("This is the second card"));
}
JPanel card3=new JPanel();
if (r[2].isSelected() )
{
card3.add(new JLabel("This is the third card"));
}
JPanel card4=new JPanel();
if (r[1].isSelected() )
{
card4.add(new JLabel("This is the forth card"));
}
p2=new JPanel();
layout=new CardLayout();
p2.setLayout(layout);
p2.add("Card 1",card1);
p2.add("Card 2",card2);
p2.add("Card 3",card3);
p2.add("Card 4",card4);
pane.add("Center",p2);
}
public void actionPerformed(ActionEvent e)
{
layout.show(p2,(String) e.getActionCommand());
}
private JPanel p1,p2;
private CardLayout layout;
} 展开
2个回答
展开全部
今天太晚了 明天帮你看 -、-
这种类型的代码用NetBeans来编写的话很方便 因为可以直接拖组件 说不定你直接不用提问也能知道怎么做了
这种类型的代码用NetBeans来编写的话很方便 因为可以直接拖组件 说不定你直接不用提问也能知道怎么做了
追问
因为我们老师说要把下面的程序改成单选按钮的,我不会改,我把原来的程序也给你看一下吧,麻烦你了,明天中午之前可以么?下午之前就交了……谢谢了,成功了会加10分的。
追答
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class EventDemo1 extends JApplet implements ActionListener {
String university[] = { "Card 1", "Card 2", "Card 3", "Card 4" };
JRadioButton r[] = new JRadioButton[4];
JLabel lab = new JLabel("");
public void init() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
p1 = new JPanel();
ButtonGroup radioGroup = new ButtonGroup();
for (int i = 0; i < 4; i++) {
r[i] = new JRadioButton(university[i]);
radioGroup.add(r[i]);
p1.add(r[i]);
}
p1.add(lab);
pane.add("North", p1);
rHandler h = new rHandler();
r[0].addItemListener(h);
r[1].addItemListener(h);
r[2].addItemListener(h);
r[3].addItemListener(h);
}
public void actionPerformed(ActionEvent e) {
layout.show(p2, (String) e.getActionCommand());
}
private JPanel p1, p2; private CardLayout layout;
private class rHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (r[0].isSelected()) {
lab.setText("This is the first card");
}
if (r[1].isSelected()) {
lab.setText("This is the second card");
}
if (r[2].isSelected()) {
lab.setText("This is the third card");
}
if (r[3].isSelected()) {
lab.setText("This is the forth card");
}
}
}
}
我不知道你是不是要做出这个效果
按钮或者lab的位置可以用setBounds(xx,xx,xx,xx);的方法控制。
今天上午有事没法做,只好早上先帮你弄掉。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询