Skip to content

Instantly share code, notes, and snippets.

@nokok
Last active August 29, 2015 13:56
Show Gist options
  • Save nokok/9307178 to your computer and use it in GitHub Desktop.
Save nokok/9307178 to your computer and use it in GitHub Desktop.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main {
public static void main(String[] args) {
new Main().run();
}
private void run() {
JFrame frame = new JFrame("Swing Test");
JPanel rootBorderLayoutPanel = new JPanel(new BorderLayout());
JPanel centerPanel = new JPanel();
final JButton button1 = new JButton("ボタン1");
button1.setOpaque(true); //trueにするとコンポーネントの境界まで全部描画する。デフォルトはfalse
button1.setBorderPainted(false); //falseにすると枠線を描画しなくなる。デフォルトはtrue
button1.setBackground(new Color(52, 152, 219));
button1.setForeground(new Color(236, 240, 241));
button1.setFont(new Font("", Font.BOLD, 13));
button1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
button1.setForeground(button1.getForeground().darker());
}
@Override
public void mouseReleased(MouseEvent e) {
button1.setForeground(button1.getForeground().brighter());
}
@Override
public void mouseEntered(MouseEvent e) {
button1.setBackground(button1.getBackground().darker());
}
@Override
public void mouseExited(MouseEvent e) {
button1.setBackground(button1.getBackground().brighter());
}
});
centerPanel.add(button1);
centerPanel.add(new JButton("ボタン2"));
rootBorderLayoutPanel.add(centerPanel, BorderLayout.CENTER);
frame.add(rootBorderLayoutPanel);
frame.pack();
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment