いちばん失敗した人決定戦

public class PlotOver extends JFrame implements ActionListener {


   XYLineAndShapeRenderer render;
Random rand = new Random();

JFrame frame2;
XYSeriesCollection trace;
ChartPanel cpane;
int no = 30;

//データを入れる配列
int data [] = new int [no];
int data1 [] = new int [no];
int data2 [] = new int [no];

//データの作成
public void makeData (int [] data) {
for (int i = 0; i < no; i++) {
data [i] = rand.nextInt(100) + 1;
}
}

//Seriesへのadd
public void seriesAdd (XYSeries a, int [] data) {
for (int i = 0; i < no; i++) {
a.add(i, data[i]);
}
}

//最初の折れ線グラフを描く
public void MakeFig () {
makeData(data);
frame2 = new JFrame ("ShowData");
frame2.setSize(500, 500);
frame2.setLocationRelativeTo(null);
frame2.setVisible (true);

trace = new XYSeriesCollection ();
XYSeries series = new XYSeries("Test");

seriesAdd(series, data);
trace.addSeries(series);

JFreeChart chart = ChartFactory.createXYLineChart (
"Data",
"Frame",
"Value",
trace,
PlotOrientation.VERTICAL,
true,
false,
false);

XYPlot linePlot = chart.getXYPlot();

render = new XYLineAndShapeRenderer();
linePlot.setRenderer(0, render);
render.setSeriesPaint (0, ChartColor.BLACK);

cpane = new ChartPanel(chart);
frame2.add(cpane);
}

//2つ目の折れ線グラフの追加
public void add1 () {
makeData(data1);
XYSeries series1 = new XYSeries("Test1");
seriesAdd(series1, data1);
render.setSeriesPaint (1, ChartColor.BLUE);
trace.addSeries(series1);
frame2.add(cpane);
}

//3つ目の折れ線グラフの追加
public void add2 () {
makeData(data2);
XYSeries series2 = new XYSeries("Test2");
seriesAdd(series2, data2);
render.setSeriesPaint (2, ChartColor.RED);
render.setSeriesStroke(2, new BasicStroke(5.0f));
trace.addSeries(series2);
frame2.add(cpane);
}



public void actionPerformed (ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("fig")) {
MakeFig();
} else if (cmd.equals("1")) {
add1();
} else if (cmd.equals("2")) {
add2();
}
}

PlotOver (String title) {
setTitle (title);
setBounds (10, 10, 240, 170);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JButton button = new JButton ("MakeFig");
JButton button1 = new JButton ("Add1");
JButton button2 = new JButton ("Add2");
button.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button.setActionCommand ("fig");
button1.setActionCommand("1");
button2.setActionCommand("2");
button.setBounds (60, 10, 100, 30);
button1.setBounds (60, 50, 100, 30);
button2.setBounds(60, 90, 100, 30);
JPanel pane = new JPanel ();
pane.setLayout(null);
pane.add(button);
pane.add(button1);
pane.add(button2);
getContentPane().add(pane, BorderLayout.CENTER);
}

public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
PlotOver frame = new PlotOver ("OverWrite");
frame.setVisible (true);
}
}

このプログラムの
public void actionPerformed (ActionEvent e) {省略}

PlotOver (String title) {〜省略}
の中身はなにをしていますか?
特にActionListenerが分かりません

回答よろしくお願いします。

A 回答 (1件)

PlotOver (String title) {



PlotOver frame = new PlotOver ("OverWrite");した時に呼び出されるコンストラクタであり

全てを表示するウインドウをサイズ指定して作る
setTitle (title);
setBounds (10, 10, 240, 170);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

三種類のボタンを作り、それぞれを見分けるidとしてfig,1,2を割り当てる
JButton button = new JButton ("MakeFig");
JButton button1 = new JButton ("Add1");
JButton button2 = new JButton ("Add2");
button.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button.setActionCommand ("fig");
button1.setActionCommand("1");
button2.setActionCommand("2");

ボタンそれぞれの配置と幅縦長さを決める
button.setBounds (60, 10, 100, 30);
button1.setBounds (60, 50, 100, 30);
button2.setBounds(60, 90, 100, 30);

パネルを作りボタン三つを張る
JPanel pane = new JPanel ();
pane.setLayout(null);
pane.add(button);
pane.add(button1);
pane.add(button2);

パネルをウインドウの真ん中に張る
getContentPane().add(pane, BorderLayout.CENTER);






public void actionPerformed (ActionEvent e)

上で設定したボタンのどれかが押されたらこのメソッドが実行される

ボタンが押された時、「どのボタンから呼び出されたか」を示す値が入っているString値を取得しcmdに代入
String cmd = e.getActionCommand();

figを値として設定したボタンが押されたなら MakeFig();を実行する
if (cmd.equals("fig")) {
MakeFig();

1を値として設定したボタンが押されたなら add1();を実行する
} else if (cmd.equals("1")) {
add1();

2を値として設定したボタンが押されたなら add2();を実行する
} else if (cmd.equals("2")) {
add2();
}
    • good
    • 0
この回答へのお礼

丁寧に教えていただきありがとうございます!!

お礼日時:2016/12/07 04:19

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!