JAVA,哪位可以帮我个忙,把下面的程序加入单选按钮(就是让你通过按钮选4中颜色),再从文档中读取文字
哪位大神可以帮我个忙,把下面的程序加入单选按钮(就是让你通过按钮选4种颜色),再从文档中读取文字在最后显示出来,文字内容是“一颗漂亮的圣诞树诞生了”importjava....
哪位大神可以帮我个忙,把下面的程序加入单选按钮(就是让你通过按钮选4种颜色),再从文档中读取文字在最后显示出来,文字内容是“一颗漂亮的圣诞树诞生了”
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Christ {
JFrame frame=new JFrame("圣诞树");
JButton bt=new JButton("确认");
JTextField numfield=new JTextField();
JTextField colorfield=new JTextField();
JTextArea ta=new JTextArea(5,5);
JLabel lb=new JLabel("");
JLabel lb1=new JLabel("请输入您需要的圣诞树的大小<数字>");
JLabel lb2=new JLabel("<html>请输入您需要的圣诞树的颜色<br>1-黄色,2-红色,3-绿色,4-蓝色</html>");
public static void main(String[] args) {
Christ temp=new Christ();
temp.go();
}
String ChristmasTree(int n){
int i, j, k, m;
String str=new String("");
for (i = 1; i <= n / 2; i++) {
for (j = 1; j < n / 2 + i; j++) {
if (j <= n / 2 - i)
str+=" ";
else {
if (j % 2 == 0)
str+="*";
else
str+="$";
}
}
str+="\n";
}
for (k = n / 2 + 1; k <= n - n / 4; k++) {
for (m = 1; m <= n + 1; m++) {
if (m != n / 2)
str+=" ";
else
str+="||";
}
str+="\n";
}
return str;
}
void go(){
JPanel fpanel=new JPanel();
JPanel zpanel=new JPanel();
fpanel.setLayout(new GridLayout(3,3));
zpanel.setLayout(new GridLayout(2,1));
fpanel.add(lb1);
fpanel.add(numfield);
fpanel.add(lb2);
fpanel.add(colorfield);
fpanel.add(lb);
fpanel.add(bt);
zpanel.add(fpanel);
JScrollPane jsp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
zpanel.add(jsp);
frame.setContentPane(zpanel);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String num=numfield.getText();
String color=colorfield.getText();
int colo=Integer.parseInt(color);
int number=Integer.parseInt(num);
switch(colo){
case 1:ta.setForeground(Color.yellow);break;
case 2:ta.setForeground(Color.red);break;
case 3:ta.setForeground(Color.green);break;
case 4:ta.setForeground(Color.blue);break;
}
Christ tempp= new Christ();
ta.append(tempp.ChristmasTree(2*number));
}
});
frame.setSize(500,400);
frame.setVisible(true);
}
} 展开
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Christ {
JFrame frame=new JFrame("圣诞树");
JButton bt=new JButton("确认");
JTextField numfield=new JTextField();
JTextField colorfield=new JTextField();
JTextArea ta=new JTextArea(5,5);
JLabel lb=new JLabel("");
JLabel lb1=new JLabel("请输入您需要的圣诞树的大小<数字>");
JLabel lb2=new JLabel("<html>请输入您需要的圣诞树的颜色<br>1-黄色,2-红色,3-绿色,4-蓝色</html>");
public static void main(String[] args) {
Christ temp=new Christ();
temp.go();
}
String ChristmasTree(int n){
int i, j, k, m;
String str=new String("");
for (i = 1; i <= n / 2; i++) {
for (j = 1; j < n / 2 + i; j++) {
if (j <= n / 2 - i)
str+=" ";
else {
if (j % 2 == 0)
str+="*";
else
str+="$";
}
}
str+="\n";
}
for (k = n / 2 + 1; k <= n - n / 4; k++) {
for (m = 1; m <= n + 1; m++) {
if (m != n / 2)
str+=" ";
else
str+="||";
}
str+="\n";
}
return str;
}
void go(){
JPanel fpanel=new JPanel();
JPanel zpanel=new JPanel();
fpanel.setLayout(new GridLayout(3,3));
zpanel.setLayout(new GridLayout(2,1));
fpanel.add(lb1);
fpanel.add(numfield);
fpanel.add(lb2);
fpanel.add(colorfield);
fpanel.add(lb);
fpanel.add(bt);
zpanel.add(fpanel);
JScrollPane jsp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
zpanel.add(jsp);
frame.setContentPane(zpanel);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String num=numfield.getText();
String color=colorfield.getText();
int colo=Integer.parseInt(color);
int number=Integer.parseInt(num);
switch(colo){
case 1:ta.setForeground(Color.yellow);break;
case 2:ta.setForeground(Color.red);break;
case 3:ta.setForeground(Color.green);break;
case 4:ta.setForeground(Color.blue);break;
}
Christ tempp= new Christ();
ta.append(tempp.ChristmasTree(2*number));
}
});
frame.setSize(500,400);
frame.setVisible(true);
}
} 展开
4个回答
展开全部
package test;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Christ {
JFrame frame=new JFrame("圣诞树");
JButton bt=new JButton("确认");
JTextField numfield=new JTextField();
JTextField colorfield=new JTextField();
JTextArea ta=new JTextArea(5,5);
JLabel lb=new JLabel("");
JLabel lb1=new JLabel("请输入您需要的圣诞树的大小<数字>");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton("黄");
JRadioButton jRadioButton2 = new JRadioButton("红");
JRadioButton jRadioButton3 = new JRadioButton("绿");
JRadioButton jRadioButton4 = new JRadioButton("蓝");
JLabel lb2=new JLabel("<html>请输入您需要的圣诞树的颜色<br>1-黄色,2-红色,3-绿色,4-蓝色</html>");
public static void main(String[] args)
{
Christ temp=new Christ ();
temp.go();
}
String ChristmasTree(int n)
{
int i, j, k, m;
String str=new String("");
for (i = 1; i <= n / 2; i++)
{
for (j = 1; j < n / 2 + i; j++)
{
if (j <= n / 2 - i)
str+=" ";
else {
if (j % 2 == 0)
str+="*";
else
str+="$";
}
}
str+="\n";
}
for (k = n / 2 + 1; k <= n - n / 4; k++)
{
for (m = 1; m <= n + 1; m++)
{
if (m != n / 2)
str+=" ";
else
str+="||";
}
str+="\n";
}
return str;
}
void go()
{
JPanel fpanel=new JPanel();
JPanel zpanel=new JPanel();
fpanel.setLayout(new GridLayout(3,3));
zpanel.setLayout(new GridLayout(2,1));
fpanel.add(lb1);
fpanel.add(numfield);
fpanel.add(lb2);
fpanel.add(colorfield);
fpanel.add(lb);
fpanel.add(bt);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
buttonGroup.add(jRadioButton4);
fpanel.add(jRadioButton1);
fpanel.add(jRadioButton2);
fpanel.add(jRadioButton3);
fpanel.add(jRadioButton4);
zpanel.add(fpanel);
JScrollPane jsp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); zpanel.add(jsp);
frame.setContentPane(zpanel);
bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String num=numfield.getText();
//String color=colorfield.getText();
//int colo=Integer.parseInt(color);
int number=Integer.parseInt(num);
int chooscolo=0;
if(buttonGroup.isSelected(buttonGroup.getSelection())){
if(jRadioButton1.isSelected())
{
chooscolo=1;
}
if(jRadioButton2.isSelected())
{
chooscolo=2;
}
if(jRadioButton3.isSelected())
{
chooscolo=3;
}
if(jRadioButton4.isSelected())
{
chooscolo=4;
}
}else
{
System.out.println("没有选中任何颜色");
}
System.out.println(buttonGroup.isSelected(buttonGroup.getSelection()));
System.out.println(jRadioButton1.isSelected());
switch(chooscolo)
{
case 1:ta.setForeground(Color.yellow);break;
case 2:ta.setForeground(Color.red);break;
case 3:ta.setForeground(Color.green);break;
case 4:ta.setForeground(Color.blue);break;
}
Christ tempp= new Christ ();
ta.append(tempp.ChristmasTree(2*number));
}
});
frame.setSize(500,400);frame.setVisible(true);
}
}
没有调整布局, 你自己可以调整一下 ,还有代码不要全写在main中 太乱,要合理分类
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Christ {
JFrame frame=new JFrame("圣诞树");
JButton bt=new JButton("确认");
JTextField numfield=new JTextField();
JTextField colorfield=new JTextField();
JTextArea ta=new JTextArea(5,5);
JLabel lb=new JLabel("");
JLabel lb1=new JLabel("请输入您需要的圣诞树的大小<数字>");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton("黄");
JRadioButton jRadioButton2 = new JRadioButton("红");
JRadioButton jRadioButton3 = new JRadioButton("绿");
JRadioButton jRadioButton4 = new JRadioButton("蓝");
JLabel lb2=new JLabel("<html>请输入您需要的圣诞树的颜色<br>1-黄色,2-红色,3-绿色,4-蓝色</html>");
public static void main(String[] args)
{
Christ temp=new Christ ();
temp.go();
}
String ChristmasTree(int n)
{
int i, j, k, m;
String str=new String("");
for (i = 1; i <= n / 2; i++)
{
for (j = 1; j < n / 2 + i; j++)
{
if (j <= n / 2 - i)
str+=" ";
else {
if (j % 2 == 0)
str+="*";
else
str+="$";
}
}
str+="\n";
}
for (k = n / 2 + 1; k <= n - n / 4; k++)
{
for (m = 1; m <= n + 1; m++)
{
if (m != n / 2)
str+=" ";
else
str+="||";
}
str+="\n";
}
return str;
}
void go()
{
JPanel fpanel=new JPanel();
JPanel zpanel=new JPanel();
fpanel.setLayout(new GridLayout(3,3));
zpanel.setLayout(new GridLayout(2,1));
fpanel.add(lb1);
fpanel.add(numfield);
fpanel.add(lb2);
fpanel.add(colorfield);
fpanel.add(lb);
fpanel.add(bt);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
buttonGroup.add(jRadioButton4);
fpanel.add(jRadioButton1);
fpanel.add(jRadioButton2);
fpanel.add(jRadioButton3);
fpanel.add(jRadioButton4);
zpanel.add(fpanel);
JScrollPane jsp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); zpanel.add(jsp);
frame.setContentPane(zpanel);
bt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String num=numfield.getText();
//String color=colorfield.getText();
//int colo=Integer.parseInt(color);
int number=Integer.parseInt(num);
int chooscolo=0;
if(buttonGroup.isSelected(buttonGroup.getSelection())){
if(jRadioButton1.isSelected())
{
chooscolo=1;
}
if(jRadioButton2.isSelected())
{
chooscolo=2;
}
if(jRadioButton3.isSelected())
{
chooscolo=3;
}
if(jRadioButton4.isSelected())
{
chooscolo=4;
}
}else
{
System.out.println("没有选中任何颜色");
}
System.out.println(buttonGroup.isSelected(buttonGroup.getSelection()));
System.out.println(jRadioButton1.isSelected());
switch(chooscolo)
{
case 1:ta.setForeground(Color.yellow);break;
case 2:ta.setForeground(Color.red);break;
case 3:ta.setForeground(Color.green);break;
case 4:ta.setForeground(Color.blue);break;
}
Christ tempp= new Christ ();
ta.append(tempp.ChristmasTree(2*number));
}
});
frame.setSize(500,400);frame.setVisible(true);
}
}
没有调整布局, 你自己可以调整一下 ,还有代码不要全写在main中 太乱,要合理分类
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不太明白你这句话,再从文档中读取文字在最后显示出来,文字内容是“一颗漂亮的圣诞树诞生了”能否解释下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码 写的和狗屎一样。。。这么简单东西还出错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
天啊~~~这。。。。路过吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询