1

I'd like to custom a little bit the basic JSpinner of swing java API. Basically, I want to move the textfield component initially located on the left of the arrow buttons. Instead, the texfield would be lcoated between the two arrows of the spinner, so that there's one arrow on top of the textfield and one arrow below the texfield. But I don't know how to proceed...

Anyone would have an idea?

4

2 回答 2

4

You might be able to override the setLayout(LayoutManager) method of JSpinner to use a custom LayoutManager.

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SpinnerLayoutTest {
  public JComponent makeUI() {
    SpinnerNumberModel m = new SpinnerNumberModel(10, 0, 1000, 1);
    JSpinner spinner = new JSpinner(m) {
      @Override public void setLayout(LayoutManager mgr) {
        super.setLayout(new SpinnerLayout());
      }
    };
    JPanel p = new JPanel(new BorderLayout(5,5));
    p.add(new JSpinner(m), BorderLayout.NORTH);
    p.add(spinner, BorderLayout.SOUTH);
    p.setBorder(BorderFactory.createEmptyBorder(16,16,16,16));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new SpinnerLayoutTest().makeUI());
    f.setSize(320, 160);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
class SpinnerLayout extends BorderLayout {
  @Override public void addLayoutComponent(Component comp, Object constraints) {
    if("Editor".equals(constraints)) {
      constraints = "Center";
    } else if("Next".equals(constraints)) {
      constraints = "North";
    } else if("Previous".equals(constraints)) {
      constraints = "South";
    }
    super.addLayoutComponent(comp, constraints);
  }
}
于 2012-08-12T15:47:09.830 回答
1

You could make an JPanel and put a JTextField and the JButtons inside it. That's was soulution, when I had the same problem.

EDIT:

Arrowbuttons can you create with:

new javax.swing.plaf.basic.BasicArrowButton(SwingConstants.NORTH);

Where SwingConstants.NORTH says that the arrow in the button is pointing upwards

于 2012-08-10T12:07:31.810 回答