你可以。。。
使用
JButton
相反,这是他们设计的目的。。。
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TestPane extends JPanel {
public TestPane() throws IOException {
setLayout(new GridLayout(-1, 3));
for (int index = 1; index < 13; index++) {
BufferedImage image = ImageIO.read(getClass().getResource("/images/Cute" + index + ".png"));
Image scaled = image.getScaledInstance(50, -1, Image.SCALE_SMOOTH);
JButton btn = new JButton(new ImageIcon(scaled));
btn.putClientProperty("image", image);
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
BufferedImage img = (BufferedImage) btn.getClientProperty("image");
// Do stuff
}
});
}
}
}
}
看见
How to Use Buttons, Check Boxes, and Radio Buttons
不想要按钮“填充”效果吗?
这变得有点复杂,但并非不可能。。。
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.AbstractBorder;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TestPane extends JPanel {
public TestPane() throws IOException {
setLayout(new GridLayout(-1, 3));
for (int index = 1; index < 13; index++) {
BufferedImage image = ImageIO.read(getClass().getResource("/images/Cute" + index + ".png"));
Image scaled = image.getScaledInstance(50, -1, Image.SCALE_SMOOTH);
JButton btn = new JButton();
btn.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
// Focus.color is for MacOS, Button.focus should work under
// most other look and feels
btn.setBorder(new RoundBorder());
}
@Override
public void focusLost(FocusEvent e) {
btn.setBorder(new EmptyBorder(1, 1, 1, 1));
}
});
btn.setBorder(new EmptyBorder(1, 1, 1, 1));
btn.setIcon(new ImageIcon(scaled));
btn.putClientProperty("image", image);
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
BufferedImage img = (BufferedImage) btn.getClientProperty("image");
// Do stuff
}
});
}
}
public class RoundBorder extends AbstractBorder {
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setColor(UIManager.getColor("Focus.color"));
g2d.drawRoundRect(x, y, width - 1, height - 1, 20, 20);
}
}
}
}
为什么要用按钮?
-
它们是为这个目的而建造的;他们表现出专注;他们在“采取行动”时提供反馈,从而导致
-
免费支持键盘输入;你可以
标签
在按钮之间,用键盘“操作”它们——你甚至可以不费吹灰之力地设置键盘快捷键。
但是
你可以。。。
使用
JList
,这将允许您使用箭头键进行导航
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TestPane extends JPanel {
public TestPane() throws IOException {
setLayout(new BorderLayout());
DefaultListModel<BufferedImage> model = new DefaultListModel<>();
for (int index = 1; index < 13; index++) {
BufferedImage image = ImageIO.read(getClass().getResource("/images/Cute" + index + ".png"));
model.addElement(image);
}
JList list = new JList(model);
list.setCellRenderer(new BufferedImageListRenderer());
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(4);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
BufferedImage image = (BufferedImage)list.getSelectedValue();
// Do stuff
}
});
list.setSelectedIndex(0);
add(new JScrollPane(list));
}
protected class BufferedImageListRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof BufferedImage) {
setText(null);
// This is expensive, I'd normally have it cached somewhere
Image scaled = ((BufferedImage)value).getScaledInstance(50, -1, Image.SCALE_SMOOTH);
setIcon(new ImageIcon(scaled));
}
return this;
}
}
}
}
看见
How to Use Lists