Java 带有自定义UI的JSpinner
我正在开发定制设计的swing base应用程序。任务之一-自定义微调器控件。我想实现这样的目标:Java 带有自定义UI的JSpinner,java,swing,jspinner,Java,Swing,Jspinner,我正在开发定制设计的swing base应用程序。任务之一-自定义微调器控件。我想实现这样的目标: 我的主要问题是微调器控件与其边界重叠: 创建微调器的代码: public class GolfSpinner extends JSpinner { public GolfSpinner() { setBorder(new LineBorder(new Color(Colors.INPUT_BORDER.getR() , Colors.I
我的主要问题是微调器控件与其边界重叠:
创建微调器的代码:
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());
}
}
}
我曾尝试更改微调器的首选大小,但没有帮助。您可能想看看这些格子是如何做到的
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
类SwingSpinnerLook{
公共静态void main(字符串[]args){
Runnable r=新的Runnable(){
@凌驾
公开募捐{
最终的JPanel gui=新的JPanel(新的BorderLayout());
JSpinner微调器=新的JSpinner微调器(
新喷丝头型号(10,1100,1);
添加(微调器、边框布局、线条结束);
最终UIManager.LookAndFeelInfo[]plafInfos=
UIManager.getInstalledLookAndFeels();
字符串[]plafNames=新字符串[plafInfos.length];
对于(int ii=0;ii谢谢你的建议!我会检查它,如果你想要设计-发布代码。现在我通过在箭头按钮上使用MatteBorder找到了临时解决方案。它不是完美的,但总比没有好。
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);
}
}