无法在 JPanel 按钮内调用 JPanel
进行了搜索,我确实没有看到与这个棘手问题相匹配的答案。
我来了,伙计们。提前感谢您的帮助。
我正在尝试使用 actionlistener 在 JPanel 的按钮内调用 JPanel; 虽然它与我添加的第一个按钮配合得很好,但对其他两个按钮却不起作用; 第一个按钮中调用的 jpanels 与其余两个按钮之间的区别在于, 第一个按钮从当前 Jpanel 中调用另一个 Jpanel;但另外两个打电话 当前 Jpanels 中的两个私有级 jpanel。
我正在做什么:我正在尝试使用在同一个 Jpanel 中显示两个不同的图表 两个按钮,可互换;私有的jpanels纯粹用于绘制图表;我想我只需要在不同的按钮中调用这两个 jpanel,而不需要创建一个全新的 JPanel 来 包含每个 jpanel 来绘制图表,然后像我在第一个按钮中调用另一个 jpanel 一样调用它们。
为我的尴尬措辞道歉......这是代码。
公共类 ExamPerf 扩展 JPanel {
最终 JPanel viewPerf = TabbedQuiz.viewPerfPanel;
最终 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("当前性能"); currentPerf.setFont(new Font("Tahoma", Font.BOLD, 11)); currentPerf.setBounds(303, 24, 151, 23); 添加(当前性能); 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));
}
}
}
非常感谢你们,有任何不清楚的地方请向我提出。
Done a search and I really didn't see a matched answer with this tricky problem.
Here I come, guys. Thanks for your help in advance.
I am trying to call a JPanel within a JPanel's button by using actionlistener;
while it worked fine with the first button I added, it doesn't work for the other two;
the difference between the jpanels called in first button and the rest two is that,
the first button calling another Jpanel out of current Jpanel; but the other two calling
two private-classed jpanel within current Jpanels.
What I am doing: I am trying display two different charts within the same Jpanel using
two buttons, interchangeably; the private jpanels are purely used for drawing charts; I thought I only need to call the these two jpanels in different buttons without creating a totally new JPanel to
contain each jpanel to draw a chart, and then call them like I call the another jpanel in the first button.
apologise for my awkward wording..here are the codes.
public class ExamPerf extends JPanel {
final JPanel viewPerf = TabbedQuiz.viewPerfPanel;
final ChartsPanel CP = new ChartsPanel();
//I am building up the jpanel in its constructor; all the buttons are in here;
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);
// this is the first button, calling another Jpanel, a completely different class;
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);
}
});
//this is the second button, I am trying to call the private class DrawCurrent;
//but it doesn't work; my thought was,
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);
}
});
//this is third button, same problem.
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);
}
});
}
// this is the first private class, DrawCurrent;
public class DrawCurrent extends 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);
}
}
// this is the second private class, DrawHistorical;
private class DrawHistorical extends 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));
}
}
}
Thank you very much guys, anything not clear please throw them at me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到堆栈溢出。这里有一些规则 - 我在这里呆了这么久,我不记得它们了:) - 但我记得有一个叫做 SSCE 的东西。一个简单、独立的示例,可确保您从每个人那里得到大量答案。
1)实际的问题是您在动作侦听器中创建新面板:
并在这里做奇怪的事情。
1)setVisible(false)将从屏幕上隐藏一些东西 - 不是我认为的面板,但可能是外部面板。
2) 创建一个新的 DrawCurrent 实例,并将其添加到布局中,而不是隐藏其他面板并使该面板可见。
正确的逻辑是:在 ExamPerf 类中同时创建 DrawCurrent 和 DrawHistorical 实例,将 add 添加到其布局中:
并且在按钮侦听器中,只需更改图表面板的可见属性:(
并且相同对于另一个按钮,这使得 true 和 false 显示另一个面板)。
或者,不要实施您自己的解决方法,而是考虑使用 CardLayout,它旨在解决此问题。
https://docs.oracle.com/javase/tutorial/uiswing/布局/card.html
Welcome to Stack Overflow. There are some rules here - I am here for so long that I dunno them by heart :) - but I remember to something called SSCE. A simple, self-contained example, which ensures that you get lots of answers from everyone.
1) The actual problem is that you create new panels right in the action listener:
and do strange things here.
1) setVisible(false) will hide something from the screen - not the panel I presume, but likely the outer panel.
2) you create a new DrawCurrent instance, and add to the layout, instead of hiding the other panel and making this panel visible.
The right logic is: create both the DrawCurrent and DrawHistorical instances at once in the ExamPerf class, add add to its layout:
And in the button listeners, just change visible properties of the chart panels:
(and same for the other button, which makes true and false to show the other panel).
Alternatively, instead of implementing your own workaround, consider using CardLayout which is designed to solve this out of the box.
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html