Java 仅在JFrame内重新绘制特定的JPanel
我正在尝试为Java 仅在JFrame内重新绘制特定的JPanel,java,swing,jframe,jpanel,repaint,Java,Swing,Jframe,Jpanel,Repaint,我正在尝试为面板1JPanel创建一个绘制组件,但我不知道怎么做。这是我的课 此代码有效,但它不绘制面板\u 1。 如果我尝试将其添加到面板中而不对其进行喷漆,则效果很好。 我还需要该面板以给定的间隔自动刷新(重新绘制)。 请让我帮忙D import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.eve
面板1
JPanel
创建一个绘制组件,但我不知道怎么做。这是我的课
此代码有效,但它不绘制面板\u 1
。
如果我尝试将其添加到面板中而不对其进行喷漆,则效果很好。
我还需要该面板以给定的间隔自动刷新(重新绘制)。
请让我帮忙D
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class GUI extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JButton btnStop, btnStart;
private JComboBox comboBox_1, comboBox;
private Manager manager = new Manager();
private String arrMin, arrMax, svcMin, svcMax, simTime;
private JTextField textField_4;
private JPanel panel_1 = new JPanel(){
@Override
public void paintComponent(Graphics g){
paintComponents(g);
panel_1.setBackground(Color.orange);
panel_1.setVisible(true);
System.out.println("Just testing");
}
};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
setResizable(false);
setForeground(Color.DARK_GRAY);
setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 500);
contentPane = new JPanel();
contentPane.setForeground(Color.DARK_GRAY);
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(12, 12, 434, 346);
panel.setForeground(Color.LIGHT_GRAY);
contentPane.add(panel);
panel.setLayout(null);
JLabel lblArrivingTime = new JLabel("Arriving time");
lblArrivingTime.setBounds(12, 367, 94, 15);
lblArrivingTime.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblArrivingTime);
JLabel lblMin = new JLabel("Min");
lblMin.setBounds(12, 395, 70, 15);
lblMin.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMin);
JLabel lblMax = new JLabel("Max");
lblMax.setBounds(12, 418, 70, 15);
lblMax.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMax);
textField = new JTextField();
textField.setBounds(58, 394, 48, 16);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(58, 422, 48, 15);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblServiceTime = new JLabel("Service Time");
lblServiceTime.setBounds(152, 367, 105, 15);
lblServiceTime.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblServiceTime);
JLabel lblMin_1 = new JLabel("Min");
lblMin_1.setBounds(152, 395, 51, 15);
lblMin_1.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMin_1);
JLabel lblMax_1 = new JLabel("Max");
lblMax_1.setBounds(152, 418, 70, 15);
lblMax_1.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMax_1);
textField_2 = new JTextField();
textField_2.setBounds(209, 393, 48, 17);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(209, 417, 48, 17);
contentPane.add(textField_3);
textField_3.setColumns(10);
JLabel lblSimSetup = new JLabel("Sim. Setup");
lblSimSetup.setBounds(292, 367, 94, 15);
lblSimSetup.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblSimSetup);
JLabel lblQueue = new JLabel("Queue");
lblQueue.setBounds(292, 395, 70, 15);
lblQueue.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblQueue);
String[] numbers = { "1", "2", "3", "4" };
comboBox = new JComboBox(numbers);
comboBox.setBounds(345, 395, 75, 15);
contentPane.add(comboBox);
JLabel lblMode = new JLabel("Mode");
lblMode.setBounds(292, 418, 70, 15);
lblMode.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblMode);
String[] mode = { "Time", "Number" };
comboBox_1 = new JComboBox(mode);
comboBox_1.setBounds(345, 413, 75, 15);
contentPane.add(comboBox_1);
JLabel lblActions = new JLabel("Actions");
lblActions.setBounds(484, 367, 70, 15);
lblActions.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblActions);
btnStart = new JButton("Start");
btnStart.setBounds(482, 392, 81, 20);
contentPane.add(btnStart);
btnStop = new JButton("Stop");
btnStop.setBounds(482, 415, 81, 20);
contentPane.add(btnStop);
JLabel lblTime = new JLabel("Time");
lblTime.setBounds(292, 440, 70, 15);
lblTime.setForeground(Color.LIGHT_GRAY);
contentPane.add(lblTime);
textField_4 = new JTextField();
textField_4.setBounds(345, 438, 75, 17);
contentPane.add(textField_4);
textField_4.setColumns(10);
panel_1.setBounds(484, 12, 202, 346);
contentPane.add(panel_1);
panel_1.setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(158, 12, -126, 322);
panel_1.add(textArea);
btnStop.addActionListener(this);
btnStart.addActionListener(this);
comboBox.addActionListener(this);
comboBox_1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStop) {
this.dispose();
System.exit(NORMAL);
} else if (e.getSource() == btnStart) {
String nrQueues = (String) comboBox.getSelectedItem();
arrMin = textField.getText();
arrMax = textField_1.getText();
svcMin = textField_2.getText();
svcMax = textField_3.getText();
simTime = textField_4.getText();
int aMin = 0;
int aMax = 0;
int sMin = 0;
int sMax = 0;
int sTime = 0;
int nQueues = 1;
try {
nQueues = Integer.parseInt(nrQueues);
aMin = Integer.parseInt(arrMin);
aMax = Integer.parseInt(arrMax);
sMin = Integer.parseInt(svcMin);
sMax = Integer.parseInt(svcMax);
sTime = Integer.parseInt(simTime);
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String s = (String) comboBox_1.getSelectedItem();
System.out.println("Selected: " + s);
boolean mode = true;
if (s.equals("Time")) {
mode = true;
} else {
mode = false;
}
manager.startSimulation(aMin * 1000, aMax * 1000, sMin * 1000,
sMax * 1000, sTime * 1000, nQueues, mode);
}
}
}
当然,处理程序在这种情况下并不相关,因为我只对绘制面板感兴趣。您正在调用
paintComponent
覆盖中的super.paintComponents
方法。哎呀。您也在使用null布局,不应该这样做
您正在设置背景色,但不使用它。您应该改为在JPanel对象上设置背景色,而不是在其paintComponent方法中设置背景色
i、 这里
panel_1.setBackground(Color.orange); // here
// !! panel_1.setBounds(484, 12, 202, 346); // you shouldn't need this!
contentPane.add(panel_1);
编辑
你说: 仅仅在文本区域中添加一个字符串就足够了,它会自动显示在屏幕上吗
是的,只要考虑到摆动线程规则。您正在调用
paintComponents
覆盖中的super.paintComponents
方法。哎呀。您也在使用null布局,不应该这样做
您正在设置背景色,但不使用它。您应该改为在JPanel对象上设置背景色,而不是在其paintComponent方法中设置背景色
i、 这里
panel_1.setBackground(Color.orange); // here
// !! panel_1.setBounds(484, 12, 202, 346); // you shouldn't need this!
contentPane.add(panel_1);
编辑
你说: 仅仅在文本区域中添加一个字符串就足够了,它会自动显示在屏幕上吗 是的,只要考虑到Swing线程规则。首先,这
private JPanel panel_1 = new JPanel(){
@Override
public void paintComponent(Graphics g){
paintComponents(g);
panel_1.setBackground(Color.orange);
panel_1.setVisible(true);
System.out.println("Just testing");
}
};
这不是定制绘画的目的。决不能从任何绘制方法内部更改零部件或任何零部件的状态
这可能会触发一个新的重新绘制请求,这将使您进入一个永不结束的绘制周期
调用paintComponents(g)
将生成一个堆栈溢出异常
。您不应该以任何方式调用paintComponents
(注意末尾的s
),但应该调用super.paintComponent(g)代码>
默认情况下,JPanel
可见
就目前而言,你根本不需要做任何你想做的事情
不要使用null
布局。Swing设计用于布局管理器,如果没有布局管理器,您将无法解决屏幕更新的问题。此外,像素完美的布局在现代用户界面设计中是一种幻觉,您无法控制字体,渲染管道或目标系统的其他方面,这些方面可能会影响文本等大型元素的渲染程度
查看更多详细信息首先,此
private JPanel panel_1 = new JPanel(){
@Override
public void paintComponent(Graphics g){
paintComponents(g);
panel_1.setBackground(Color.orange);
panel_1.setVisible(true);
System.out.println("Just testing");
}
};
这不是定制绘画的目的。决不能从任何绘制方法内部更改零部件或任何零部件的状态
这可能会触发一个新的重新绘制请求,这将使您进入一个永不结束的绘制周期
调用paintComponents(g)
将生成一个堆栈溢出异常
。您不应该以任何方式调用paintComponents
(注意末尾的s
),但应该调用super.paintComponent(g)代码>
默认情况下,JPanel
可见
就目前而言,你根本不需要做任何你想做的事情
不要使用null
布局。Swing设计用于布局管理器,如果没有布局管理器,您将无法解决屏幕更新的问题。此外,像素完美的布局在现代用户界面设计中是一种幻觉,您无法控制字体,渲染管道或目标系统的其他方面,这些方面可能会影响文本等大型元素的渲染程度
查看更多详细信息我知道我可以简单地做到这一点,但我想使用paint方法,因为在该文本区域中,我将显示一个日志文件,该文件需要每秒更新一次。仅仅在文本区域添加一个字符串就足够了,它会自动显示在屏幕上吗?@user2448690:对不起,我正试图理解这一点,但我无法理解它的头绪。绘制包含JTextArea的JPanel与每秒在JTextArea中获取更新有什么关系?我看不出这两者是如何联系在一起的。我不知道这是不够的。我会尽力去做的。谢谢你的帮助。@ USE2448 690:不客气,并考虑学习布局管理器。否则,您可能会创建不灵活的GUI,除了您的平台之外,其他所有平台上的GUI都很糟糕。我知道我可以简单地做到这一点,但我想使用paint方法,因为在该文本区域中,我将显示一个日志文件,该文件需要每秒更新一次。仅仅在文本区域添加一个字符串就足够了,它会自动显示在屏幕上吗?@user2448690:对不起,我正试图理解这一点,但我无法理解它的头绪。绘制包含JTextArea的JPanel与每秒在JTextArea中获取更新有什么关系?我看不出这两者是如何联系在一起的。我不知道这是不够的。我会尽力去做的。谢谢你的帮助。@ USE2448 690:不客气,并考虑学习布局管理器。否则,您可能会创建不灵活的GUI,而这些GUI在您的平台上看起来很糟糕。