完成搜索,我真的没有看到这个棘手问题的匹配答案。
我来了,伙计们。感谢您的帮助。
我试图通过使用actionlistener在JPanel的按钮中调用JPanel; 虽然它添加的第一个按钮工作正常,但它不适用于其他两个按钮; 在第一个按钮和其余两个按钮中调用的jpanel之间的区别是, 第一个按钮调用当前Jpanel的另一个Jpanel;但另外两个叫 目前的Jpanels中有两个私人的jpanel。
我在做什么:我正在尝试在同一个Jpanel中使用显示两个不同的图表 两个按钮,可互换;私人jpanels纯粹用于绘制图表;我想我只需要在不同的按钮中调用这两个jpanel,而无需创建一个全新的JPanel 包含每个jpanel以绘制图表,然后像在第一个按钮中调用另一个jpanel一样调用它们。
为我笨拙的措辞道歉......代码也有。
公共类ExamPerf扩展了JPanel {
final JPanel viewPerf = TabbedQuiz.viewPerfPanel;
final ChartsPanel CP = new ChartsPanel();
//我在构造函数中构建jpanel;所有按钮都在这里;
public ExamPerf() {
setBackground(Color.BLACK);
setBounds(0, 0, 728, 380);
setLayout(null);
setBorder(BorderFactory.createLineBorder(Color.ORANGE, 10));
setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.ORANGE),
"Exam Performance", 0, 0, new Font("Tahoma", Font.BOLD, 15),
Color.WHITE));
DrawCurrent drawarea=new DrawCurrent();
add(drawarea);
//这是第一个按钮,调用另一个Jpanel,一个完全不同的类;
JButton backBut = new JButton("Main Menu");
backBut.setFont(new Font("Tahoma", Font.BOLD, 11));
backBut.setBounds(10, 24, 93, 23);
add(backBut);
backBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
invalidate();
viewPerf.add(CP);
}
});
//这是第二个按钮,我试图调用私有类DrawCurrent; //但它不起作用;我的想法是,
JButton histoPerf = new JButton("Historical Performance");
histoPerf.setFont(new Font("Tahoma", Font.BOLD, 11));
histoPerf.setBounds(122, 24, 161, 23);
add(histoPerf);
histoPerf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
setVisible(false);
invalidate();
DrawCurrent drawplace=new DrawCurrent();
add(drawplace, BorderLayout.CENTER);
}
});
//这是第三个按钮,同样的问题。
JButton currentPerf = new JButton("Current Performance"); currentPerf.setFont(new Font("Tahoma", Font.BOLD, 11)); currentPerf.setBounds(303, 24, 151, 23); add(currentPerf); currentPerf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
setVisible(true);
invalidate();
DrawHistorical drawHis=new DrawHistorical();
add(drawHis);
}
});
}
//这是第一个私有类,DrawCurrent;
公共类DrawCurrent扩展了JPanel {
public DrawCurrent() {
setBackground(Color.BLACK);
setForeground(null);
setBounds(10, 49, 708, 320);
setBorder(BorderFactory.createLineBorder(Color.ORANGE, 10));
setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.ORANGE), "Chart", 0,
0, new Font("Tahoma", Font.BOLD, 15), Color.WHITE));
setLayout(null);
}
@Override
public void paintComponent(Graphics g) {
final Graphics2D canvas = (Graphics2D) g;
final BasicStroke Pen = new BasicStroke(4, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
canvas.setStroke(Pen);
canvas.setColor(Color.LIGHT_GRAY);
canvas.drawLine(5, 315, 703, 315);
canvas.drawLine(getWidth() / 2, 700, getWidth() / 2, 19);
// write the names of columns on top of them;
canvas.setFont(new Font("Times New Roman", Font.BOLD, 13));
canvas.drawString("Right", 50, 100);
canvas.drawString("Wrong", getWidth() / 2 + 150, 300);
// draw two 3D rectangles as the columns of the performances;
//
canvas.fill3DRect(50, 100, 40, 215, true);
canvas.fill3DRect(getWidth() / 2 + 150, 300, 40, 15, true);
}
}
//这是第二个私有类,DrawHistorical;
私有类DrawHistorical扩展了JPanel {
final ArrayList<Integer> examCount=new ArrayList<>();
final int leftEdge=10;
public DrawHistorical() {
setBackground(Color.BLACK);
setForeground(null);
setBounds(leftEdge, 49, 708, 320);
setBorder(BorderFactory.createLineBorder(Color.ORANGE, 10));
setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.ORANGE), "Chart", 0,
0, new Font("Tahoma", Font.BOLD, 15), Color.WHITE));
setLayout(null);
}
@Override
public void paintComponent(Graphics g) {
final Graphics2D canvas = (Graphics2D) g;
final BasicStroke Pen = new BasicStroke(2, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
canvas.setStroke(Pen);
canvas.setColor(Color.LIGHT_GRAY);
canvas.setFont(new Font("Times New Roman", Font.BOLD, 13));
//this loop is to draw the graph of the historical performance of completed exams.
for(int i=0; i<examCount.size();i=i+3){
// write the number of exam on top of the lien graph.
canvas.drawString("Ex"+examCount.get(i).toString(), leftEdge+i, examCount.get(i));
// draw the line graph and extends as long as the number of completed exams increases.
canvas.drawLine(leftEdge+2+i, examCount.get(i), leftEdge+3+i, examCount.get(i+1));
}
}
}
非常感谢你们,任何不清楚的事请把它们扔给我。
答案 0 :(得分:0)
欢迎使用Stack Overflow。这里有一些规则 - 我在这里待了很长时间,我心不在焉:)但我还记得一个叫做SSCE的东西。一个简单,独立的示例,确保您从每个人那里获得大量答案。
1)实际问题是你在动作监听器中创建了新面板:
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
invalidate();
DrawCurrent drawplace=new DrawCurrent();
add(drawplace, BorderLayout.CENTER);
}
在这里做些奇怪的事情。
1)setVisible(false)会隐藏屏幕上的内容 - 不是我认为的面板,而是外面板。
2)您创建一个新的DrawCurrent实例,并添加到布局中,而不是隐藏其他面板并使该面板可见。
正确的逻辑是:在ExamPerf类中同时创建DrawCurrent和DrawHistorical实例,在其布局中添加add:
DrawCurrent current;
DrawHistorical historical;
public ExamPerf() extends JPanel {
current=new DrawCurrent();
historical=new Historical();
add(current); // TODO you may need further layout settings here
add(historical); // TODO you may need further layout settings here
current.setVisible(true);
historical.setVisible(false);
}
在按钮监听器中,只需更改图表面板的可见属性:
public void actionPerformed(ActionEvent arg0) {
current.setVisible(false);
history.setVisible(true);
}
(对于另一个按钮也是如此,这使得true和false显示另一个面板)。
或者,请考虑使用旨在解决此问题的CardLayout,而不是实施您自己的解决方法。
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html