下面列出了javax.swing.JButton#getPreferredSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Dimension getMinimumSize(JComponent c) {
Dimension d = super.getMinimumSize(c);
// if it's not a large variance: use the height of
// a button. This way our combobox and buttons are a uniform
// height when they're in the same row.
if (buttonMinSize == null) {
JButton button = new JButton("X");
button.setUI(buttonUI);
buttonMinSize = button.getPreferredSize();
}
int distance = Math.abs(d.height - buttonMinSize.height);
if (distance < 5)
d.height = Math.min(d.height, buttonMinSize.height);
return d;
}
private ExprEditor(final boolean booleanExpected) {
button = new JButton("...");
final Dimension preferredSize = button.getPreferredSize();
preferredSize.setSize(25, preferredSize.getHeight());
button.setPreferredSize(preferredSize);
value = new String[1];
final ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final int i = editExpression(value, booleanExpected);
if (i == ModalDialog.ID_OK) {
fireEditingStopped();
} else {
fireEditingCanceled();
}
}
};
button.addActionListener(actionListener);
}
private JButton createButton1(String name, int x, int y) {
JButton btn = new CustomButton(StringResource.get(name));
btn.setBackground(ColorResource.getDarkBtnColor());
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setForeground(Color.WHITE);
btn.setFont(FontResource.getNormalFont());
Dimension d = btn.getPreferredSize();
btn.setBounds(x, y, d.width, d.height);
btn.addActionListener(this);
return btn;
}
private JButton createButton1(String name, int x, int y) {
JButton btn = new CustomButton(StringResource.get(name));
btn.setBackground(ColorResource.getDarkBtnColor());
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setForeground(Color.WHITE);
btn.setFont(FontResource.getNormalFont());
Dimension d = btn.getPreferredSize();
btn.setBounds(x, y, d.width, d.height);
// btn.addActionListener(this);
return btn;
}
private JButton createButton1(String name, int x, int y) {
JButton btn = new CustomButton(StringResource.get(name));
btn.setBackground(ColorResource.getDarkBtnColor());
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setForeground(Color.WHITE);
btn.setFont(FontResource.getNormalFont());
Dimension d = btn.getPreferredSize();
btn.setBounds(x, y, d.width, d.height);
btn.addActionListener(this);
return btn;
}
private void initToolbar() {
Insets ins = actionsBar.getMargin();
JButton dummy = new JButton();
dummy.setBorderPainted(false);
dummy.setOpaque(false);
dummy.setText(null);
dummy.setIcon(new Icon() {
@Override
public int getIconHeight() {
return 16;
}
@Override
public int getIconWidth() {
return 16;
}
@SuppressWarnings(value = "empty-statement")
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
;
}
});
actionsBar.add(dummy);
Dimension buttonPref = dummy.getPreferredSize();
Dimension minDim = new Dimension(buttonPref.width + ins.left + ins.right, buttonPref.height + ins.top + ins.bottom);
actionsBar.setMinimumSize(minDim);
actionsBar.setPreferredSize(minDim);
actionsBar.remove(dummy);
actionsBar.setBorder(new RightBorder());
actionsBar.setBorderPainted(true);
}
private void fixSize(JToolBar actionBar) {
Insets ins = actionBar.getMargin();
JButton dummy = new JButton();
dummy.setBorderPainted(false);
dummy.setOpaque(false);
dummy.setText(null);
dummy.setIcon(new Icon() {
@Override
public int getIconHeight() {
return 16;
}
@Override
public int getIconWidth() {
return 16;
}
@SuppressWarnings(value = "empty-statement")
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
;
}
});
actionBar.add(dummy);
Dimension buttonPref = dummy.getPreferredSize();
Dimension minDim = new Dimension(buttonPref.width + ins.left + ins.right, buttonPref.height + ins.top + ins.bottom);
actionBar.setMinimumSize(minDim);
actionBar.setPreferredSize(minDim);
actionBar.remove(dummy);
}
public CPanelTwoButtons(final ActionListener listener, final String firstButtonName,
final String secondButtonName) {
super();
m_FirstButtonName = firstButtonName;
m_SecondButtonName = secondButtonName;
setLayout(new BorderLayout());
m_FirstButton = new JButton(m_FirstButtonName);
m_SecondButton = new JButton(m_SecondButtonName);
m_FirstButton.addActionListener(listener);
m_SecondButton.addActionListener(listener);
if (m_FirstButton.getPreferredSize().width > m_SecondButton.getPreferredSize().width) {
m_SecondButton.setPreferredSize(m_FirstButton.getPreferredSize());
} else {
m_FirstButton.setPreferredSize(m_SecondButton.getPreferredSize());
}
final JPanel p = new JPanel(new GridLayout(1, 2));
final JPanel p_ok = new JPanel();
final JPanel p_cancel = new JPanel();
p_ok.setBorder(new EmptyBorder(5, 5, 5, 5));
p_cancel.setBorder(new EmptyBorder(5, 5, 5, 5));
p_ok.add(m_FirstButton);
p_cancel.add(m_SecondButton);
p.add(p_ok);
p.add(p_cancel);
add(p, BorderLayout.EAST);
}
public CPanelTwoButtons(final ActionListener listener, final String firstButtonName,
final String secondButtonName) {
super();
m_listener = listener;
m_FirstButtonName = firstButtonName;
m_SecondButtonName = secondButtonName;
setLayout(new BorderLayout());
m_FirstButton = new JButton(m_FirstButtonName);
m_SecondButton = new JButton(m_SecondButtonName);
m_FirstButton.addActionListener(listener);
m_SecondButton.addActionListener(listener);
if (m_FirstButton.getPreferredSize().width > m_SecondButton.getPreferredSize().width) {
m_SecondButton.setPreferredSize(m_FirstButton.getPreferredSize());
} else {
m_FirstButton.setPreferredSize(m_SecondButton.getPreferredSize());
}
final JPanel p = new JPanel(new GridLayout(1, 2));
final JPanel p_ok = new JPanel();
final JPanel p_cancel = new JPanel();
p_ok.setBorder(new EmptyBorder(5, 5, 5, 5));
p_cancel.setBorder(new EmptyBorder(5, 5, 5, 5));
p_ok.add(m_FirstButton);
p_cancel.add(m_SecondButton);
p.add(p_ok);
p.add(p_cancel);
add(p, BorderLayout.EAST);
}
public FileChooserPanel(final String defaultText, final ActionListener listener,
final String buttonText, final int width, final int height, final int buttonWidth) {
super(new BorderLayout());
setBorder(new LineBorder(Color.GRAY));
inputField = new JTextField(defaultText);
inputField.setEditable(false);
if ((width > 0) || (height > 0)) {
setPreferredSize(new Dimension(width, height));
}
final JPanel p1extBt = new JPanel(new BorderLayout());
browseButton = new JButton(buttonText);
browseButton.setBorder(new MatteBorder(0, 1, 0, 0, Color.GRAY));
final Dimension prefSide = browseButton.getPreferredSize();
browseButton.setPreferredSize(new Dimension(prefSide.width + 15, prefSide.height));
if (buttonWidth > 0) {
browseButton.setPreferredSize(new Dimension(buttonWidth, height));
}
p1extBt.add(browseButton, BorderLayout.CENTER);
browseButton.setFocusable(false);
add(inputField, BorderLayout.CENTER);
add(p1extBt, BorderLayout.EAST);
browseButton.addActionListener(listener);
ToolTipManager.sharedInstance().registerComponent(inputField);
inputField.setToolTipText(getText());
}
private JComponent buildContentPane() {
JPanel panel = new JPanel(null);
int x = 16;
int y = 16;
Dimension size;
textArea = new JTextArea("Type your document here.", 12, 25);
textArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
size = textArea.getPreferredSize();
textArea.setBounds(x, y, size.width, size.height);
panel.add(textArea);
x += size.width + 6;
saveButton = new JButton("Save...");
size = saveButton.getPreferredSize();
saveButton.setBounds(x, y, size.width, size.height);
panel.add(saveButton);
y += size.height + 4;
openButton = new JButton("Open...");
size = openButton.getPreferredSize();
openButton.setBounds(x, y, size.width, size.height);
panel.add(openButton);
x += size.width + 16;
y = textArea.getPreferredSize().height + 16 + 16;
panel.setPreferredSize(new Dimension(x, y));
return panel;
}
/**
* Initialize buttons
*/
private void initButtons() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton btOk = new JButton(EditorLabels.getLabel("general.OK"));
btOk.addActionListener(e -> {
if (beforeOkAction()) {
confirmed = true;
dispose();
}
});
JButton btCancel = new JButton(EditorLabels.getLabel("general.Cancel"));
btCancel.addActionListener(e -> {
if (beforeCancelAction()) {
dispose();
}
});
buttonPanel.add(btCancel);
buttonPanel.add(btOk);
int width = Math.max(btOk.getPreferredSize().width, btCancel.getPreferredSize().width);
Dimension size = new Dimension(width, btOk.getPreferredSize().height);
btOk.setPreferredSize(size);
btCancel.setPreferredSize(size);
add(buttonPanel, BorderLayout.SOUTH);
}
protected final JButton buildDialogButton(String buttonText) {
JButton button = new JButton(buttonText);
button.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent event) {
getJDialog().requestFocusInWindow();
}
});
Dimension size = button.getPreferredSize();
size.width = 75;
button.setPreferredSize(size);
return button;
}
public IOWindowImpl() {
pane = TabbedPaneFactory.createCloseButtonTabbedPane();
pane.addChangeListener(this);
pane.addPropertyChangeListener(TabbedPaneFactory.PROP_CLOSE, this);
setFocusable(true);
toolbar = new ToolbarWithOverflow();
toolbar.setOrientation(JToolBar.VERTICAL);
toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.Y_AXIS));
toolbar.setFloatable(false);
Insets ins = toolbar.getMargin();
JButton sample = new JButton();
sample.setBorderPainted(false);
sample.setOpaque(false);
sample.setText(null);
sample.setIcon(new Icon() {
public int getIconHeight() {
return 16;
}
public int getIconWidth() {
return 16;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
}
});
toolbar.add(sample);
Dimension buttonPref = sample.getPreferredSize();
Dimension minDim = new Dimension(buttonPref.width + ins.left + ins.right, buttonPref.height + ins.top + ins.bottom);
toolbar.setMinimumSize(minDim);
toolbar.setPreferredSize(minDim);
toolbar.remove(sample);
setLayout(new BorderLayout());
add(toolbar, BorderLayout.WEST);
toolbar.setBorder(new VariableRightBorder(pane));
toolbar.setBorderPainted(true);
popupMenu = new JPopupMenu();
popupMenu.add(new Close());
popupMenu.add(new CloseAll());
popupMenu.add(new CloseOthers());
pane.addMouseListener(new MouseUtils.PopupMouseAdapter() {
@Override
protected void showPopup(MouseEvent evt) {
popupMenu.show(IOWindowImpl.this, evt.getX(), evt.getY());
}
});
pane.addMouseListener(new MouseAdapter() { // #221375
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
requestActive();
}
}
});
String name = NbBundle.getMessage(IOWindow.class, "LBL_IO_WINDOW");
setDisplayName(name); //NOI18N
setToolTipText(name);
// setting name to satisfy the accesible name requirement for window.
setName(name); //NOI18N
setIcon(ImageUtilities.loadImage(ICON_RESOURCE)); // NOI18N
// special title for sliding mode
// XXX - please rewrite to regular API when available - see issue #55955
putClientProperty("SlidingName", getDisplayName()); //NOI18N
if (AQUA) {
setBackground(UIManager.getColor("NbExplorerView.background"));
setOpaque(true);
toolbar.setBackground(UIManager.getColor("NbExplorerView.background"));
pane.setBackground(UIManager.getColor("NbExplorerView.background"));
pane.setOpaque(true);
setMinimumSize(new Dimension()); // #254566
}
}
/**
* If License was not accepted during installation user must accept it here.
*/
public static void showLicensePanel() throws Exception {
Utils.setSystemLaF();
// Make sure the code following this call runs on JDK 6
if (!VisualVMStartup.checkEnv())
throw new org.openide.util.UserCancelException();
URL url = AcceptLicense.class.getResource("LICENSE.txt"); // NOI18N
LicensePanel licensePanel = new LicensePanel(url);
ResourceBundle bundle = NbBundle.getBundle(AcceptLicense.class);
String yesLabel = bundle.getString("MSG_LicenseYesButton"); // NOI18N
String noLabel = bundle.getString("MSG_LicenseNoButton"); // NOI18N
JButton yesButton = new JButton();
JButton noButton = new JButton();
Utils.setLocalizedText(yesButton, yesLabel);
Utils.setLocalizedText(noButton, noLabel);
ActionListener listener = new ActionListener () {
public void actionPerformed(ActionEvent e) {
command = e.getActionCommand();
d.setVisible(false);
d.dispose();
d = null;
}
};
yesButton.addActionListener(listener);
noButton.addActionListener(listener);
yesButton.setActionCommand(YES_AC);
noButton.setActionCommand(NO_AC);
yesButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_AcceptButton")); // NOI18N
yesButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSD_AcceptButton")); // NOI18N
noButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_RejectButton")); // NOI18N
noButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSD_RejectButton")); // NOI18N
Dimension yesPF = yesButton.getPreferredSize();
Dimension noPF = noButton.getPreferredSize();
int maxWidth = Math.max(yesPF.width, noPF.width);
int maxHeight = Math.max(yesPF.height, noPF.height);
yesButton.setPreferredSize(new Dimension(maxWidth, maxHeight));
noButton.setPreferredSize(new Dimension(maxWidth, maxHeight));
d = StartupDialog.create(bundle.getString("MSG_LicenseDlgTitle"), null, -1); // NOI18N
d.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_LicenseDlg")); // NOI18N
d.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LicenseDlg")); // NOI18N
d.getContentPane().add(licensePanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(17, 12, 11, 11));
buttonPanel.add(yesButton);
buttonPanel.add(noButton);
d.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
d.setSize(new Dimension(600, 600));
d.setResizable(true);
d.setLocationRelativeTo(null);
d.setVisible(true);
if (!YES_AC.equals(command)) throw new UserCancelException();
}
protected LayoutBlueprint(boolean initialPermitLocked) {
super(ANIMATION_DURATION, 1F);
this.initialPermitLocked = initialPermitLocked;
Insets insets = getInsets();
int height = getHeight() - insets.top - insets.bottom;
int remainingHeight = height;
float totalVerticalWeight = 0;
Map<JComponent, Number> verticalWeightMap = new HashMap<JComponent, Number>();
for (int a = 0; a < sections.size(); a++) {
Section section = sections.get(a);
JPanel body = section.getBody();
JButton header = getHeader(section);
Boolean collapsed = (Boolean) header
.getClientProperty(COLLAPSED);
if (collapsed == null)
collapsed = Boolean.FALSE;
if (!header.isVisible())
collapsed = true;
components.add(header);
components.add(body);
if (header.isVisible())
visibleComponents.add(header);
if ((!collapsed))
visibleComponents.add(body);
Number n = (Number) section.getProperty(VERTICAL_WEIGHT);
if (n == null)
n = 0;
if (visibleComponents.contains(body)) {
totalVerticalWeight += n.floatValue();
if (n.floatValue() != 0)
verticalWeightMap.put(body, n);
}
if (visibleComponents.contains(header)) {
Dimension headerSize = header.getPreferredSize();
heightMap.put(header, headerSize.height);
remainingHeight -= headerSize.height;
} else {
heightMap.put(header, 0);
}
originalHeightMap.put(header, header.getHeight());
if (visibleComponents.contains(body) && n.floatValue() == 0) {
Dimension bodySize = body.getPreferredSize();
heightMap.put(body, bodySize.height);
remainingHeight -= bodySize.height;
} else {
heightMap.put(body, 0);
}
originalHeightMap.put(body, body.getHeight());
}
if (remainingHeight > 0 && totalVerticalWeight > 0) {
// divide the remaining height based on the vertical weight
int designatedHeight = 0;
JComponent lastC = null;
for (JComponent jc : verticalWeightMap.keySet()) {
Number weight = verticalWeightMap.get(jc);
int i = (int) (weight.floatValue() / totalVerticalWeight * remainingHeight);
heightMap.put(jc, heightMap.get(jc) + i);
designatedHeight += i;
lastC = jc;
}
// due to rounding error, we may have a few extra pixels:
int remainingPixels = remainingHeight - designatedHeight;
// tack them on to someone. anyone.
heightMap.put(lastC, heightMap.get(lastC) + remainingPixels);
}
}