JAVA 框架内的按钮生成图表
JAVA button inside a frame generates chart
这是我第一次 post 来到这里,但危急时刻需要孤注一掷。我的问题是我制作了一个带有按钮和其他按钮的 JFrame 来执行我的项目(每个下一个按钮都会在单击前一个按钮时显示(代码包含 if 和 else)。当倒数第二个按钮(最后一个 else)被点击时,它会执行项目的最后一部分并显示,让我们称之为,"Create Chart"按钮。我想要做的是在单击此按钮后显示新框架,这将在新框架中显示图表。这是我的代码(缩短和简化只是为了展示这个想法):
public class clazz extends JFrame
{
static JButton // my buttons
static JLabel //my labels
static JCheckBox // my checkBoxes
static JTextField // my textfields
public double //declaration of my variables
public clazz()
{
setSize(1600,900);
setTitle("Project");
setLayout(null);
// created and set up lots of buttons/labels/textfields etc.
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button1();}});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button2();}});
// and it continues like that
}
public void Button1()
{
//sth
}
public void Button2()
{
//sth
}
// and continues like that
public static void main(String[] args)
{
clazz aplication = new clazz();
aplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aplication.setVisible(true);
}
}
出于我的问题的目的,假设 button3 将是创建图表的按钮(在另一个框架中)。我应该怎么做才能根据以前的按钮生成的数据获得带有图表的新框架。我看到的所有其他主题都使用新的 class 绘制图表,而那些对我来说并不复杂(我是一条鱼)。
感谢您的帮助!
编辑
完成!如果你知道我感兴趣的更简单的方法。
// the beginning is the same
// to the last button in the constructor (let's say the third one) I've added method chartt()
// here's the method chartt()
public void chartt() {
JFrame frame = new JFrame();
XYSeries DATA = new XYSeries("");
matrix1 = new double[641];
matrix2 = new double[641];
for (int x=0; x<=640; x++)
{
matrix1[x]= pressure(x*1000); //pressure is defined in another method with argument x
matrix2[x] = x;
}
for (int y=0;y<=640;y++)
{
DATA.add(matrix2[y], matrix1[y]);
}
XYDataset xyDataset = new XYSeriesCollection(DATA);
JFreeChart chart = ChartFactory.createXYLineChart(
"", "", "",
xyDataset, PlotOrientation.VERTICAL, true, true, false);
ChartPanel cp = new ChartPanel(chart)
{
public Dimension getPreferredSize()
{
return new Dimension(600, 600);
}
};
cp.setMouseWheelEnabled(true);
add(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
frame.add(cp);
frame.setVisible(true);
}
祝大家好运!
我从你的问题中得到的是,当你点击最后一个按钮时,另一个 JFrame(可能你已经设计了它)让必须显示 XYZ 的名称。
XYZ 将包含您设计的图表。
所以在 ActionPerformed
事件上你需要编写以下代码:
XYZ x = new XYZ();
x.setVisible(true);
this.dispose(); // use this line if you wish to close the first frame with buttons.
希望对您有所帮助。如果您还有其他问题,请在评论中提及。
这是我第一次 post 来到这里,但危急时刻需要孤注一掷。我的问题是我制作了一个带有按钮和其他按钮的 JFrame 来执行我的项目(每个下一个按钮都会在单击前一个按钮时显示(代码包含 if 和 else)。当倒数第二个按钮(最后一个 else)被点击时,它会执行项目的最后一部分并显示,让我们称之为,"Create Chart"按钮。我想要做的是在单击此按钮后显示新框架,这将在新框架中显示图表。这是我的代码(缩短和简化只是为了展示这个想法):
public class clazz extends JFrame
{
static JButton // my buttons
static JLabel //my labels
static JCheckBox // my checkBoxes
static JTextField // my textfields
public double //declaration of my variables
public clazz()
{
setSize(1600,900);
setTitle("Project");
setLayout(null);
// created and set up lots of buttons/labels/textfields etc.
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button1();}});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button2();}});
// and it continues like that
}
public void Button1()
{
//sth
}
public void Button2()
{
//sth
}
// and continues like that
public static void main(String[] args)
{
clazz aplication = new clazz();
aplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aplication.setVisible(true);
}
}
出于我的问题的目的,假设 button3 将是创建图表的按钮(在另一个框架中)。我应该怎么做才能根据以前的按钮生成的数据获得带有图表的新框架。我看到的所有其他主题都使用新的 class 绘制图表,而那些对我来说并不复杂(我是一条鱼)。
感谢您的帮助!
编辑
完成!如果你知道我感兴趣的更简单的方法。
// the beginning is the same
// to the last button in the constructor (let's say the third one) I've added method chartt()
// here's the method chartt()
public void chartt() {
JFrame frame = new JFrame();
XYSeries DATA = new XYSeries("");
matrix1 = new double[641];
matrix2 = new double[641];
for (int x=0; x<=640; x++)
{
matrix1[x]= pressure(x*1000); //pressure is defined in another method with argument x
matrix2[x] = x;
}
for (int y=0;y<=640;y++)
{
DATA.add(matrix2[y], matrix1[y]);
}
XYDataset xyDataset = new XYSeriesCollection(DATA);
JFreeChart chart = ChartFactory.createXYLineChart(
"", "", "",
xyDataset, PlotOrientation.VERTICAL, true, true, false);
ChartPanel cp = new ChartPanel(chart)
{
public Dimension getPreferredSize()
{
return new Dimension(600, 600);
}
};
cp.setMouseWheelEnabled(true);
add(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
frame.add(cp);
frame.setVisible(true);
}
祝大家好运!
我从你的问题中得到的是,当你点击最后一个按钮时,另一个 JFrame(可能你已经设计了它)让必须显示 XYZ 的名称。 XYZ 将包含您设计的图表。
所以在 ActionPerformed
事件上你需要编写以下代码:
XYZ x = new XYZ();
x.setVisible(true);
this.dispose(); // use this line if you wish to close the first frame with buttons.
希望对您有所帮助。如果您还有其他问题,请在评论中提及。