frame.setPreferredSize(new Dimension(600, 400)); final JToolBar toolBar = new JToolBar(); toolBar.add(button); frame.getContentPane().add(toolBar, BorderLayout.NORTH); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true);
this.setPreferredSize(new Dimension(320, 200)); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true);
private static void forceSize(Component c, int width, int height) { Dimension d = new Dimension(width, height); c.setSize(d); c.setMinimumSize(d); c.setMaximumSize(d); c.setPreferredSize(d); }
String param = getParameter("param"); System.out.println("parameter: " + param); add(new JLabel(param)); pa.setPreferredSize(new java.awt.Dimension(200,200)); JOptionPane.showMessageDialog(null, pa);
tip1Null.setFont(new Font("Serif", Font.BOLD, 14)); tip1Null.setForeground(Color.darkGray); tip1Null.setPreferredSize(new Dimension(50, 30)); tip1Null.addActionListener(new java.awt.event.ActionListener() { @Override frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tip1Null, BorderLayout.CENTER); frame.setLocation(150, 150); frame.setPreferredSize(new Dimension(310, 75)); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true);
private void forceSize(Component c, int width, int height) { c.setSize(width, height); Dimension d = new Dimension(width, height); c.setMinimumSize(d); c.setMaximumSize(d); c.setPreferredSize(d); } }
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JFXPanel fxPanel = new JFXPanel(); frame.add(jButton); frame.add(fxPanel); frame.setVisible(true); jButton.setSize(new Dimension(200, 27)); fxPanel.setSize(new Dimension(300, 300)); fxPanel.setLocation(new Point(0, 27)); frame.getContentPane().setPreferredSize(new Dimension(300, 327)); frame.pack(); frame.setResizable(false);
private void forceSize(Component c, int width, int height) { c.setSize(width, height); Dimension d = new Dimension(width, height); c.setMinimumSize(d); c.setMaximumSize(d); c.setPreferredSize(d); }
txtURL.addActionListener(al); progressBar.setPreferredSize(new Dimension(150, 18)); progressBar.setStringPainted(true); topBar.add(txtURL, BorderLayout.CENTER); topBar.add(btnGo, BorderLayout.EAST); statusBar.add(lblStatus, BorderLayout.CENTER); statusBar.add(progressBar, BorderLayout.EAST); frame.setPreferredSize(new Dimension(1024, 600)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true);
public void setPreferredSize(Component component, int size) { switch (this) { case RIGHT: case LEFT: component.setPreferredSize(new Dimension(size, 1)); return; default: component.setPreferredSize(new Dimension(1, size)); } }
JPanel bottomPanel = CreateBottomPanel(); frame = new JFrame("Comp Table Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(CompTableScrollpane, BorderLayout.CENTER); frame.add(bottomPanel, BorderLayout.SOUTH); frame.setPreferredSize(new Dimension(800, 400)); frame.setLocation(150, 150); frame.pack(); frame.setVisible(true); panel.add(addButton); return panel;
/** * Sets the scaled component width, adopting the original component height. * @param comp component * @param w width */ public static void setWidth(final Component comp, final int w) { comp.setPreferredSize(new Dimension(w, comp.getPreferredSize().height)); }
import java.awt.*; import javax.swing.*; class FixedSizeContent { public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { JFrame f = new JFrame("Fixed size content"); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Container c = f.getContentPane(); c.setBackground(Color.YELLOW); // adjust to need. Dimension d = new Dimension(400,40); c.setPreferredSize(d); f.pack(); f.setResizable(false); f.setVisible(true); } }); } }
/** * Sets the scaled component height, adopting the original component width. * @param comp component * @param h height */ public static void setHeight(final Component comp, final int h) { comp.setPreferredSize(new Dimension(comp.getPreferredSize().width, h)); }
setPreferredSize(new Dimension(400, 400)); // let's open the window with a default size of 400x400 pixels getContentPane().add(splitPane); // due to the GridLayout, our splitPane will now fill the whole window inputPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75)); // we set the max height to 75 and the max width to (almost) unlimited inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS)); // X_Axis will arrange the content horizontally pack(); // calling pack() at the end, will ensure that every layout and size we just defined gets applied before the stuff becomes visible @Override public void run(){ new MyFrame().setVisible(true);
/** * Sets the scaled component width, adopting the original component height. * @param comp component * @param w width */ public static void setWidth(final Component comp, final int w) { comp.setPreferredSize(new Dimension(w, comp.getPreferredSize().height)); }
mainPanel.add(new JButton(new MyAction(frame, JDialog.DISPOSE_ON_CLOSE, "DISPOSE_ON_CLOSE"))); mainPanel.add(new JButton(new MyAction(frame, JDialog.HIDE_ON_CLOSE, "HIDE_ON_CLOSE"))); mainPanel.add(new JButton(new MyAction(frame, JDialog.DO_NOTHING_ON_CLOSE, "DO_NOTHING_ON_CLOSE"))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); dialog = new JDialog(frame, title, false); dialog.setDefaultCloseOperation(defaultCloseOp); dialog.setPreferredSize(new Dimension(300, 200)); dialog.pack(); dialog.addWindowListener(new WindowAdapter() { @Override
/** * Sets the scaled component height, adopting the original component width. * @param comp component * @param h height */ public static void setHeight(final Component comp, final int h) { comp.setPreferredSize(new Dimension(comp.getPreferredSize().width, h)); }
textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n"); JScrollPane scrollPane = new JScrollPane(textPane); frame.add(scrollPane, BorderLayout.CENTER);// Highlight some text cyanPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.cyan); redPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.red); } catch (BadLocationException ble) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(300, 200)); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true);
JComboBox comboBox = new JComboBox(); comboBox.setPrototypeDisplayValue("This is a cell's prototype text"); comboBox.setRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); int width = c.getPreferredSize().width; // let the preferred width based on prototype value int height = 40; c.setPreferredSize(new Dimension(width, height)); return c; } });