
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が分かりません
回答よろしくお願いします。

No.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();
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
2列目を右寄せにしたい
-
エラーで式の開始が不正ですと...
-
ソースコード記載のHPを教えて...
-
java junit list同士の比較方法
-
setColorメソッド の使い方
-
Buttonを押すと画像が表示され...
-
java DAO 日付指定フォーマット...
-
オブジェクトの中のプロパティ...
-
動的配列が存在(要素が有る)か...
-
C言語のポインターに関する警告
-
ORA-01858: 数値を指定する箇所...
-
JSPでの計算結果表示
-
System.err. printlnとSystem.o...
-
C#で、あるクラスのメンバーす...
-
ループ処理の際、最後だけ","を...
-
IF関数でEmpty値を設定する方法。
-
JSPやサーブレットでSystem.out...
-
javaで質問です。 文字列2023/2...
-
<forEach> 内で供給された "ite...
-
ページング
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
java junit list同士の比較方法
-
javaで文字数制限するには?
-
JPanelの重ね方/OverlayLayout他
-
ArrayListの使い方について(初...
-
TextAreaでの文字の色
-
JPanelの切り替えと再描画
-
setPrefferedSizeとsetSize
-
JFrameとJPanelの最大化について
-
【Swing】JPanel クラス外から...
-
フレームを閉じれない
-
Buttonを押すと画像が表示され...
-
JDialogを閉じた時の処理
-
BMI測定を作ろうとしている...
-
Java-Swing TextAreaにスクロー...
-
ボールが勝手に動き困ってます。
-
Javaについて
-
エラーで式の開始が不正ですと...
-
KeyListenerの使い方について
-
Enterキーでボタンを押すにはど...
-
4択クイズでのランダム出題の...
おすすめ情報