當前位置:編程學習大全網 - 編程語言 - Java編程 設計壹個圖形用戶界面。界面包括三個單選按鈕、兩個復選框、壹個列表、壹個文本區和壹個按

Java編程 設計壹個圖形用戶界面。界面包括三個單選按鈕、兩個復選框、壹個列表、壹個文本區和壹個按

程序如下:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JTextArea;

public class JFrameDemo extends JFrame implements ActionListener

{

private JPanel panel;

private JButton button;

private JTextArea textArea;

private JCheckBox musicBox;

private JCheckBox danceBox;

private JRadioButton hanButton;

private JRadioButton manButton;

private JRadioButton huiButton;

private ButtonGroup buttonGroup;

public JFrameDemo()

{

panel = new JPanel();

button = new JButton("確定");

textArea = new JTextArea(40,30);

musicBox = new JCheckBox("唱歌");

danceBox = new JCheckBox("跳舞");

huiButton = new JRadioButton("回族");

hanButton = new JRadioButton("漢族");

manButton = new JRadioButton("滿族");

buttonGroup = new ButtonGroup();

buttonGroup.add(huiButton);

buttonGroup.add(hanButton);

buttonGroup.add(manButton);

panel.setLayout(new FlowLayout(3));

panel.add(huiButton);

panel.add(hanButton);

panel.add(manButton);

panel.add(musicBox);

panel.add(danceBox);

panel.add(button);

panel.add(textArea);

add(panel);

setTitle("選擇興趣愛好");

setBounds(100, 100, 400, 280);

setResizable(false);

setVisible(true);

this.button.addActionListener(this);

}

public static void main(String[] args)

{

new JFrameDemo();

}

@Override

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == this.button)

{

String info = "";

if(this.huiButton.isSelected())

{

info += this.huiButton.getText() + "\n";

}

if(this.hanButton.isSelected())

{

info += this.hanButton.getText() + "\n";

}

if(this.manButton.isSelected())

{

info += this.manButton.getText() + "\n";

}

if(this.danceBox.isSelected())

{

info += this.danceBox.getText() + "\n";

}

if(this.musicBox.isSelected())

{

info += this.musicBox.getText() + "\n";

}

this.textArea.setText(info);

}

}

}

有問題歡迎提問,滿意請采納,謝謝!

  • 上一篇:請教如何用matlab去讀取內容不規則的文本文件的數據
  • 下一篇:plc要學習些什麽內容
  • copyright 2024編程學習大全網