JAVA作业1、用三个按钮分别代表三种标题Test\Exercise和Program,选中单选按 5
在线等答案JAVA作业谢谢大神钮后,窗口的标题改成该按钮代表的标题1、用三个按钮分别代表三种标题Test\Exercise和Program,选中单选按钮后,窗口的标题改成...
在线等答案 JAVA作业 谢谢大神 钮后,窗口的标题改成该按钮代表的标题
1、用三个按钮分别代表三种标题Test\Exercise和Program,选中单选按钮后,窗口的标题改成该按钮代表的标题
2、在窗口上显示三个滑块,分别控制窗口背景色中红绿蓝颜色比例 展开
1、用三个按钮分别代表三种标题Test\Exercise和Program,选中单选按钮后,窗口的标题改成该按钮代表的标题
2、在窗口上显示三个滑块,分别控制窗口背景色中红绿蓝颜色比例 展开
1个回答
展开全部
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestDemo extends JFrame implements ActionListener, ChangeListener
{
private ButtonGroup _group;
private JRadioButton _num1;
private JRadioButton _num2;
private JRadioButton _num3;
private JSlider _slider1;
private JSlider _slider2;
private JSlider _slider3;
public TestDemo(){
_num1 = new JRadioButton("Test");
_num2 = new JRadioButton("Exercise");
_num3 = new JRadioButton("Program");
_group = new ButtonGroup();
_slider1 = new JSlider(0, 255, 0);
_slider2 = new JSlider(0, 255, 0);
_slider3 = new JSlider(0, 255, 0);
this.setLayout(new FlowLayout(FlowLayout.CENTER, 3,3));
_group.add(_num1);
_group.add(_num2);
_group.add(_num3);
this.add(_num1);
this.add(_num2);
this.add(_num3);
this.add(_slider1);
this.add(_slider2);
this.add(_slider3);
this.setBounds(100, 100, 300, 300);
this.setVisible(true);
_num1.addActionListener(this);
_num2.addActionListener(this);
_num3.addActionListener(this);
_slider1.addChangeListener(this);
_slider2.addChangeListener(this);
_slider3.addChangeListener(this);
}
public static void main(String [] args){
TestDemo demo = new TestDemo();
String title = "";
Enumeration<AbstractButton> elements = demo._group.getElements();
while(elements.hasMoreElements()){
AbstractButton button = elements.nextElement();
if(button.isSelected()){
title = button.getText();
break;
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource() == _num1){
this.setTitle(_num1.getText());
}else if(e.getSource() == _num2){
this.setTitle(_num2.getText());
}else{
this.setTitle(_num3.getText());
}
}
@Override
public void stateChanged(ChangeEvent e)
{
// TODO Auto-generated method stub
if(e.getSource() == _slider1){
this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));
System.out.println(_slider1.getValue());
}else if(e.getSource() == _slider2){
this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));
}else{
this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));
}
repaint();
}
}
全部自己打的采纳吧
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestDemo extends JFrame implements ActionListener, ChangeListener
{
private ButtonGroup _group;
private JRadioButton _num1;
private JRadioButton _num2;
private JRadioButton _num3;
private JSlider _slider1;
private JSlider _slider2;
private JSlider _slider3;
public TestDemo(){
_num1 = new JRadioButton("Test");
_num2 = new JRadioButton("Exercise");
_num3 = new JRadioButton("Program");
_group = new ButtonGroup();
_slider1 = new JSlider(0, 255, 0);
_slider2 = new JSlider(0, 255, 0);
_slider3 = new JSlider(0, 255, 0);
this.setLayout(new FlowLayout(FlowLayout.CENTER, 3,3));
_group.add(_num1);
_group.add(_num2);
_group.add(_num3);
this.add(_num1);
this.add(_num2);
this.add(_num3);
this.add(_slider1);
this.add(_slider2);
this.add(_slider3);
this.setBounds(100, 100, 300, 300);
this.setVisible(true);
_num1.addActionListener(this);
_num2.addActionListener(this);
_num3.addActionListener(this);
_slider1.addChangeListener(this);
_slider2.addChangeListener(this);
_slider3.addChangeListener(this);
}
public static void main(String [] args){
TestDemo demo = new TestDemo();
String title = "";
Enumeration<AbstractButton> elements = demo._group.getElements();
while(elements.hasMoreElements()){
AbstractButton button = elements.nextElement();
if(button.isSelected()){
title = button.getText();
break;
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource() == _num1){
this.setTitle(_num1.getText());
}else if(e.getSource() == _num2){
this.setTitle(_num2.getText());
}else{
this.setTitle(_num3.getText());
}
}
@Override
public void stateChanged(ChangeEvent e)
{
// TODO Auto-generated method stub
if(e.getSource() == _slider1){
this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));
System.out.println(_slider1.getValue());
}else if(e.getSource() == _slider2){
this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));
}else{
this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));
}
repaint();
}
}
全部自己打的采纳吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询