[英]JAVA button inside a frame generates chart
這是我在這里的第一篇文章,但絕望的時候需要絕望的舉動。 我的問題是,我做了按鈕和其他人做我的項目一個JFrame(每一個按鈕顯示在點擊前一個時(代碼由上如果和其他人 )。當點擊倒數第二個按鈕( 否則最后一個)它完成項目的最后一部分並顯示,讓我們稱之為“創建圖表”按鈕。我想要做的是在點擊此按鈕后顯示新框架,該按鈕將在新框架中顯示圖表。這是我的代碼(縮短和簡化只是為了表明這個想法):
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將是創建圖表的那個(在另一個框架中)。 如何根據之前按鈕生成的數據獲取帶有圖表的新框架。 我看到的所有其他主題都使用一個新類來做圖表,那些不是為我復雜的(我是一條魚)。
謝謝你的幫助!
編輯
完成了! 如果你知道我感興趣的更簡單的方法。
// 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.
希望能幫助到你。 如果您還有其他問題,請在評論中提及。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.