사용자 정의 JButton
을 그릴 때 시스템 모양과 느낌을 확장했습니다. com.my.package.MyButtonUI
와* ComponentUI 클래스 (Swing 앱)에서 기본 기본 크기 (UI 다시 정의)를 설정하는 방법은 무엇입니까?
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.put("ButtonUI", "com.my.package.MyButtonUI");
:
public class MyButtonUI extends BasicButtonUI {
public static final int BTN_HEIGHT = 24;
private static final MyButtonUI INSTANCE = new MyButtonUI();
public static ComponentUI createUI(JComponent b) {
return INSTANCE;
}
@Override
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp;
if (b.isEnabled()) {
gp = new GradientPaint(0, 0, Color.red, 0, BTN_HEIGHT * 0.6f, Color.gray, true);
} else {
gp = new GradientPaint(0, 0, Color.black, 0, BTN_HEIGHT * 0.6f, Color.blue, true);
}
g2d.setPaint(gp);
g2d.fillRect(0, 0, b.getWidth(), BTN_HEIGHT);
super.paint(g, b);
}
@Override
public void update(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
b.setForeground(Color.white);
paint(g, b);
}
}
지금 내가이 버튼에 그 제약 조건을 추가하고 싶습니다 : 나는 버튼이
setPreferredSize()
가 클라이언트 코드에서 호출 된 경우를 제외하고 70x24의 크기를 갖고 싶어. 어떻게해야합니까?
참고 : MyButtonUI.update()
메서드에 setPreferredSize()
을 입력하면 클라이언트가 setPreferredSize()
을 무시하고 모든 내 버튼의 크기가 같아집니다.
감사합니다.
는 편집 : 기욤에
감사합니다,이 같은 (MyButtonUI
에) getPreferredSize()
를 오버라이드 :
@Override
public Dimension getPreferredSize(JComponent c) {
AbstractButton button = (AbstractButton) c;
int width = Math.max(button.getWidth(), BUTTON_WIDTH);
int height = Math.max(button.getHeight(), BUTTON_HEIGHT);
return new Dimension(width, height);
}
하고 그것을 잘 작동합니다.
Worked. 고맙습니다. –