Created
August 18, 2018 08:51
-
-
Save umyuu/7f35ad705b12f9562f6bc61b1dffd34d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`` | |
import java.awt.BorderLayout; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.geom.Ellipse2D; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.border.LineBorder; | |
public class SampleSwing extends JFrame implements ActionListener { | |
private JPanel buttonPanel; | |
private JLabel label1; | |
private JLabel label2; | |
private boolean paintFlag1 = false; | |
private boolean paintFlag2 = false; | |
public static void main(String[] args) { | |
SampleSwing test = new SampleSwing("SampleSwing"); | |
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
test.setVisible(true); | |
} | |
SampleSwing(String title) { | |
setTitle(title); | |
setLocationRelativeTo(null); | |
setSize(400, 400); | |
label1 = new JLabel("ボタン1:青OFF"); | |
label1.setHorizontalAlignment(JLabel.LEFT); | |
label2 = new JLabel("ボタン2:赤OFF"); | |
label2.setHorizontalAlignment(JLabel.RIGHT); | |
JButton btn1 = new JButton("Button 1"); | |
btn1.addActionListener(this); | |
btn1.setActionCommand("Button 1"); | |
JButton btn2 = new JButton("Button 2"); | |
btn2.addActionListener(this); | |
btn2.setActionCommand("Button 2"); | |
JPanel graph = new JPanel() { | |
@Override | |
public void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
if (!paintFlag1 && !paintFlag2) { | |
// return前にsuper.paintComponentを行っているのでこの位置では行わない。 | |
return; | |
} | |
Graphics2D g2 = (Graphics2D) g; | |
if (paintFlag1) { | |
g2.setColor(Color.BLUE); | |
} | |
if (paintFlag2) { | |
g2.setColor(Color.RED); | |
} | |
// 座標を変更 | |
Ellipse2D ellipse = new Ellipse2D.Double(0, 70, 150, 150); | |
g2.draw(ellipse); | |
} | |
}; | |
// JPanelのボーダー色を設定(可視化用) | |
graph.setBorder(new LineBorder(Color.CYAN)); | |
// コンストラクタでレイアウトを設定する。 | |
JPanel p = new JPanel(new BorderLayout()); | |
getContentPane().add(p, BorderLayout.CENTER); | |
p.add(btn1, BorderLayout.NORTH); | |
p.add(btn2, BorderLayout.SOUTH); | |
// actionPerformedからこちらに移動。 | |
p.add(graph, BorderLayout.CENTER); | |
p.add(label1, BorderLayout.WEST); | |
p.add(label2, BorderLayout.EAST); | |
} | |
// ボタン制御OnOff | |
// @Overrideアノテーションを付ける | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String cmd = e.getActionCommand(); | |
if (cmd.equals("Button 1")) { | |
paintFlag1 = !paintFlag1; | |
if (paintFlag1) { | |
label1.setText("ボタン1:青OFF"); | |
} else { | |
label1.setText("ボタン1:青ON"); | |
} | |
} else if (cmd.equals("Button 2")) { | |
paintFlag2 = !paintFlag2; | |
if (paintFlag2) { | |
label2.setText("ボタン2:赤OFF"); | |
} else { | |
label2.setText("ボタン2:赤ON"); | |
} | |
} | |
// これでいいのか疑問? | |
repaint(); | |
} | |
} | |
`` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment