我正在开发定制设计的swing-base应用程序。任务之一 - 自定义微调控件。我想达到这样的目的:
我的主要问题是微调控件重叠它的边框:
创建微调器的代码:
public class GolfSpinner extends JSpinner {
public GolfSpinner() {
setBorder(new LineBorder(new Color(Colors.INPUT_BORDER.getR()
, Colors.INPUT_BORDER.getG(), Colors.INPUT_BORDER.getB()), 1, true));
setEditor(new Editor(this));
setUI(new GolfSpinnerUI());
}
private class GolfSpinnerUI extends BasicSpinnerUI {
@Override
protected Component createPreviousButton() {
Component component = createButton(Icons.ARROW_DOWN.getUrl());
super.createPreviousButton();
if (component != null) {
installPreviousButtonListeners(component);
}
return component;
}
@Override
protected Component createNextButton() {
Component component = createButton(Icons.ARROW_UP.getUrl());
if (component != null) {
installNextButtonListeners(component);
}
return component;
}
private Component createButton(String url) {
BufferedImage icon = null;
try {
icon = ImageIO.read(new File(url));
} catch (IOException e) {
System.err.println(e.getMessage());
}
if (icon != null) {
JButton arrowButton = new JButton(new ImageIcon(icon));
arrowButton.setBackground(new Color(Colors.APP_PANEL_BACKGROUND.getR()
, Colors.APP_PANEL_BACKGROUND.getG(), Colors.APP_PANEL_BACKGROUND.getB()));
arrowButton.setBorderPainted(false);
arrowButton.setPreferredSize(new Dimension(icon.getWidth() + 15, icon.getHeight() + 5));
return arrowButton;
} else {
return null;
}
}
}
private class Editor extends JPanel implements ChangeListener {
private JLabel label = new JLabel();
private Editor(JSpinner spinner) {
setLayout(new FlowLayout(FlowLayout.LEADING));
label.setForeground(Color.BLACK);
setBackground(Color.WHITE);
setBorder(new EmptyBorder(5, 6, 5, 0));
add(label);
spinner.addChangeListener(this);
}
@Override
public void stateChanged(ChangeEvent e) {
JSpinner spinner = (JSpinner) e.getSource();
label.setText(spinner.getValue().toString());
}
}
}
我试图改变旋转器的首选大小,但它没有帮助。
答案 0 :(得分:3)
你可能想看看这些PLAF是如何做到的。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class SwingSpinnerLook {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
final JPanel gui = new JPanel(new BorderLayout());
JSpinner spinner = new JSpinner(
new SpinnerNumberModel(10,1,100,1));
gui.add(spinner, BorderLayout.LINE_END);
final UIManager.LookAndFeelInfo[] plafInfos =
UIManager.getInstalledLookAndFeels();
String[] plafNames = new String[plafInfos.length];
for (int ii=0; ii<plafInfos.length; ii++) {
plafNames[ii] = plafInfos[ii].getName();
}
final JComboBox<String> plafChooser = new JComboBox<String>(plafNames);
gui.add(plafChooser);
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = plafChooser.getSelectedIndex();
try {
UIManager.setLookAndFeel(
plafInfos[index].getClassName() );
SwingUtilities.updateComponentTreeUI(gui);
} catch(Exception ex) {
ex.printStackTrace();
}
}
};
plafChooser.addActionListener(al);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}