站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→如何点击单选按扭输出相应的话?

如何点击单选按扭输出相应的话?

发表新主题   回复此主题

第1楼 2007-07-15 23:31 小盗贼 写道:

如何点击单选按扭输出相应的话?

如上。
比如我打算点一下单选按扭然后输出一句话。一共4个按扭,比如按下a键输出“你好”,按下b键输出“再见”等等。应该如何弄?

第2楼 2013-08-31 12:44 Robot :

如何点击单选按扭输出相应的话? 相关


第3楼 2007-07-16 12:32 Kimmy★Piggy 写道:

package test;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JDesktopPane;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JLabel;

public class Test2 extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JRadioButton[] jRadioButton = null;
private JDesktopPane jDesktopPane = null;
private JLabel jLabel = null;

private int selectRadio = -1;

public Test2() {
super();
initialize();
}

private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);

}
return jContentPane;
}

private JRadioButton[] getJRadioButton() {
if (jRadioButton == null) {
ActionListener listener = new MyListener();
jRadioButton = new JRadioButton[4];
for(int i = 0; i < 4; i++)
jRadioButton[i] = new JRadioButton();
jRadioButton[0].setBounds(new Rectangle(30, 30, 50, 25));
jRadioButton[0].setText("a");
jRadioButton[1].setBounds(new Rectangle(100, 30, 50, 25));
jRadioButton[1].setText("b");
jRadioButton[2].setBounds(new Rectangle(30, 100, 50, 25));
jRadioButton[2].setText("c");
jRadioButton[3].setBounds(new Rectangle(100, 100, 50, 25));
jRadioButton[3].setText("d");
for(int i = 0; i < 4; i++)
jRadioButton[i].addActionListener(listener);
}
return jRadioButton;
}

private JDesktopPane getJDesktopPane() {
if (jDesktopPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(198, 63, 79, 40));
jLabel.setText("");
jDesktopPane = new JDesktopPane();
jDesktopPane.add(jLabel, null);
for(int i = 0; i < 4; i++)
jDesktopPane.add(getJRadioButton()[i], null);
}
return jDesktopPane;
}

class MyListener implements ActionListener{

public void actionPerformed(java.awt.event.ActionEvent e) {
if(selectRadio != -1) jRadioButton[selectRadio].setSelected(false);
for(int i = 0; i < 4; i++){
if(jRadioButton[i].isSelected()){
selectRadio = i;
break;
}
}
jLabel.setText(jRadioButton[selectRadio].getText());
}

}

public static void main(String[] args){
(new Test2()).setVisible(true);
}

}

第4楼 2007-07-25 12:45 xtwlq 写道:

用vb 或 delphi吧
太简单了
1、建个工程
2、建个窗体或表单
3、拉上几个按钮,分别命名
4、双击各个按钮,并生成的函数段中加入代码:
以 vb为例。
private sub command1_click()
textbox1.text = "哈哈,文字输出其不意" ' 输出到文本框
debug.print "哈哈,文字输出" ' 输出到调试栏
? " 哈哈" ' 输出到窗体表单
end sub
5、其它类推。

够明白了吧。

第5楼 2007-07-30 18:34 litandy 写道:

将下列内容保存成.html文件,打开浏览器看看:
<html>
<head>
<title>test</title>
</head>
<body>
<input type="radio" name="ra1" id="ra1" value="你好" onclick="javascript:hello();" checked="false"/>a<br/>
<input type="radio" name="ra1" id="ra1" value="谢谢" onclick="javascript:hello();" checked="false"/>b<br/>
<input type="radio" name="ra1" id="ra1" value="再见" onclick="javascript:hello();" checked="false"/>c<br/>
<input type="radio" name="ra1" id="ra1" value="对不起" onclick="javascript:hello();" checked="false"/>d<br/>
<script>
function hello(){
var or=document.getElementsByName("ra1");
for(var i=0;i<or.length;i++){
if(or[i].checked){
alert(or[i].value);
}
}
}
</script>
</body>
</html>


发表新主题   回复此主题