GUI关于paintComponent方法,
importjavax.swing.*;importjava.util.Scanner;importjava.awt.*;importjava.awt.event.Act...
import javax.swing.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Interface extends JFrame implements ActionListener{
private JPanel jp1,jp2,jp4;
private JLabel l1,l2,l3;
public JTextField t1,t2,t3;
private TextArea j1;
private JButton b1,b2,b3;
Container cc;
PP jp3;
public Interface(){
this.setBounds(0, 0,1300, 760);
this.setTitle("火车车厢重排系统");
cc=this.getContentPane();
cc.setLayout(new BorderLayout());
jp1=new JPanel(new FlowLayout(0,5,5));
cc.add(jp1,BorderLayout.NORTH);
j1=new TextArea(40,28);//文字解释区
jp2=new JPanel();
cc.add(jp2,BorderLayout.EAST);
jp2.add(j1);
l1=new JLabel("要排的车厢序列号");
l1.setForeground(new Color(109,241,179));
l1.setFont(new Font("",Font.PLAIN,20));
t1=new JTextField(12);
jp1.add(l1);
jp1.add(t1);
l2=new JLabel("缓冲轨道个数");
l2.setForeground(new Color(109,241,179));
l2.setFont(new Font("",Font.PLAIN,20));
t2=new JTextField(4);
jp1.add(l2);
jp1.add(t2);
b1=new JButton("开始");
jp1.add(b1,getLayout());
b2=new JButton("单步执行");
jp1.add(b2,getLayout());
b3=new JButton("清空");
jp1.add(b3,getLayout());
l3=new JLabel("重排后的车厢序号");
l3.setForeground(new Color(109,241,179));
l3.setFont(new Font("",Font.PLAIN,20));
t3=new JTextField(13);
jp1.add(l3);
jp1.add(t3);
j1.setEditable(false);
// 解释输出
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
jp3=new PP();
cc.add(jp3);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args){
new Interface().setVisible(true);
}
}
class PP extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Color c=g.getColor();
for(int i=0;i<10;i++){
g.setColor(Color.pink);
g.fillRect((10+40*i),300,20,20);
g.setColor(Color.black);
g.drawRect((10+40*i),300,20,20);
g.setColor(Color.blue);
g.drawString(String.valueOf(i),(15+40*i),315);
}
g.setColor(c);
}
}
在 TextField t1(1~9) ,t2(10以内随机树)中输入数据,在paintComponents()中怎么获取到用户输入在t1,t2中的数据。然后按了按钮b1后画出。求大神指导 展开
import java.util.Scanner;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Interface extends JFrame implements ActionListener{
private JPanel jp1,jp2,jp4;
private JLabel l1,l2,l3;
public JTextField t1,t2,t3;
private TextArea j1;
private JButton b1,b2,b3;
Container cc;
PP jp3;
public Interface(){
this.setBounds(0, 0,1300, 760);
this.setTitle("火车车厢重排系统");
cc=this.getContentPane();
cc.setLayout(new BorderLayout());
jp1=new JPanel(new FlowLayout(0,5,5));
cc.add(jp1,BorderLayout.NORTH);
j1=new TextArea(40,28);//文字解释区
jp2=new JPanel();
cc.add(jp2,BorderLayout.EAST);
jp2.add(j1);
l1=new JLabel("要排的车厢序列号");
l1.setForeground(new Color(109,241,179));
l1.setFont(new Font("",Font.PLAIN,20));
t1=new JTextField(12);
jp1.add(l1);
jp1.add(t1);
l2=new JLabel("缓冲轨道个数");
l2.setForeground(new Color(109,241,179));
l2.setFont(new Font("",Font.PLAIN,20));
t2=new JTextField(4);
jp1.add(l2);
jp1.add(t2);
b1=new JButton("开始");
jp1.add(b1,getLayout());
b2=new JButton("单步执行");
jp1.add(b2,getLayout());
b3=new JButton("清空");
jp1.add(b3,getLayout());
l3=new JLabel("重排后的车厢序号");
l3.setForeground(new Color(109,241,179));
l3.setFont(new Font("",Font.PLAIN,20));
t3=new JTextField(13);
jp1.add(l3);
jp1.add(t3);
j1.setEditable(false);
// 解释输出
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
jp3=new PP();
cc.add(jp3);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args){
new Interface().setVisible(true);
}
}
class PP extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Color c=g.getColor();
for(int i=0;i<10;i++){
g.setColor(Color.pink);
g.fillRect((10+40*i),300,20,20);
g.setColor(Color.black);
g.drawRect((10+40*i),300,20,20);
g.setColor(Color.blue);
g.drawString(String.valueOf(i),(15+40*i),315);
}
g.setColor(c);
}
}
在 TextField t1(1~9) ,t2(10以内随机树)中输入数据,在paintComponents()中怎么获取到用户输入在t1,t2中的数据。然后按了按钮b1后画出。求大神指导 展开
1个回答
微测检测5.10
2023-05-10 广告
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇...
点击进入详情页
本回答由微测检测5.10提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询