【发布时间】:2016-11-29 16:52:32
【问题描述】:
我想在这个程序中添加图形。图形将绘制在“drawPanel”对象上。我必须在这里使用线程。
不知道用线程绘制 Jpanel 对象。在该 Jpanel 对象上绘制图形的高效方法是什么?
线程和paintComponent() 将如何交互。谢谢。
代码:
public class LinearSearch extends JPanel{
private final Font LABEL_FONT = new Font("courier", Font.BOLD, 30);
private JPanel mainPanel;
private JPanel centerPanel;
private JPanel inputPanel;
private JPanel drawPanel;
private JLabel lblTitle;
private Button btnBack;
public LinearSearch(JPanel mainPanel) {
this.mainPanel = mainPanel;
setBackground(Color.WHITE);
initialize_All();
}
private void initialize_All() {
lblTitle = new JLabel("\"Linear Search\"", SwingConstants.CENTER);
lblTitle.setFont(LABEL_FONT);
///Center Panel
centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
///Input Panel
inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
///Draw Panel
drawPanel = new JPanel();
drawPanel.setLayout(new FlowLayout());
/// I want to add graphics on this drawPanel Jpanel
btnBack = new Button("Back");
btnBack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
removeAll();
mainPanel.add(new CatagoriesMenu(mainPanel));
}
});
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout(10, 10));
add(lblTitle, BorderLayout.PAGE_START);
add(centerPanel, BorderLayout.CENTER);
add(btnBack, BorderLayout.PAGE_END);
centerPanel.add(inputPanel, BorderLayout.PAGE_START);
centerPanel.add(drawPanel, BorderLayout.CENTER);
}
}
【问题讨论】:
-
应用程序在什么时候以及如何执行。做一个“线性搜索”?我希望动画进度的最合适方法是绘制进度条的功能等效项。例如。一个有边框的矩形,横跨大部分可视区域,填充了背景颜色。然后在该边界内填充一个较小(较窄)的矩形,表示该时刻的进度。因为搜索了 150 条记录中的 50 条,它会填满三分之一。 100人中有50人,一半。至于使用线程来做到这一点,这在很大程度上取决于您如何使用线程进行搜索..
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
-
保持您的问题无噪音,因此不要添加诸如“已添加答案”之类的内容。
标签: java swing jpanel java-threads