final JFrame frame = new JFrame("Nested Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); plafComponents.add(plafChooser); plafComponents.add(pack); SwingUtilities.updateComponentTreeUI(frame); if (pack.isSelected()) { frame.pack(); frame.setMinimumSize(frame.getSize()); gui.add(plafComponents, BorderLayout.NORTH); frame.pack(); frame.setMinimumSize(frame.getSize()); } catch(Throwable ignoreAndContinue) { frame.setVisible(true);
import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Test4 { protected static void initUI() { JFrame frame = new JFrame("test"); frame.setBounds(0, 0, 300, 200); frame.setVisible(true); System.err.println(frame.getSize()); frame.setResizable(false); System.err.println(frame.getSize()); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initUI(); } }); } }
JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); BORDER_CACHE.put(c, new CachedBorder(dropShadow, c.getSize())); if (comp.getSize().equals(size)) {
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.border.EmptyBorder;import javax.swing.filechooser.FileFilter;import javax.swing.filechooser.FileNameExtensionFilter;public class ConfigureDialog extends JDialog implements ActionListener{private static final long serialVersionUID=1L;private final JPanel contentPanel=new JPanel();private JTextField driverPathTextField;private JLabel lblDriverPath;private JButton btnBrowse;public static void main(String[]args){try{ConfigureDialog dialog=new ConfigureDialog(new JFrame());dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);dialog.setVisible(true);}catch(Exception e){e.printStackTrace();}} public ConfigureDialog(JFrame parent){super(parent,"",true);if(parent!=null){Dimension parentSize=parent.getSize();Point p=parent.getLocation();setLocation(p.x+parentSize.width+100,p.y+parentSize.height/1);} setBounds(100,100,479,141);getContentPane().setLayout(new BorderLayout());contentPanel.setBorder(new EmptyBorder(5,5,5,5));getContentPane().add(contentPanel,BorderLayout.CENTER);{lblDriverPath=new JLabel("Driver Path : ");} {driverPathTextField=new JTextField(System.getProperty("web.ie.driver"));driverPathTextField.setColumns(10);} btnBrowse=new JButton("Browse");GroupLayout gl_contentPanel=new GroupLayout(contentPanel);gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(lblDriverPath).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(btnBrowse).addComponent(driverPathTextField,GroupLayout.DEFAULT_SIZE,207,Short.MAX_VALUE)).addContainerGap()));gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addGap(5).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(driverPathTextField,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addComponent(lblDriverPath)).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(btnBrowse).addContainerGap(21,Short.MAX_VALUE)));contentPanel.setLayout(gl_contentPanel);{JPanel buttonPane=new JPanel();buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));getContentPane().add(buttonPane,BorderLayout.SOUTH);{JButton okButton=new JButton("OK");okButton.setActionCommand("OK");okButton.addActionListener(this);buttonPane.add(okButton);getRootPane().setDefaultButton(okButton);} {JButton cancelButton=new JButton("Cancel");cancelButton.setActionCommand("Cancel");cancelButton.addActionListener(this);buttonPane.add(cancelButton);}} btnBrowse.addActionListener(this);}@Override public void actionPerformed(ActionEvent e){if("Cancel".contains(e.getActionCommand())){dispose();}else if("Browse".contains(e.getActionCommand())){JFileChooser fileopen=new JFileChooser();FileFilter filter=new FileNameExtensionFilter("exe file","exe");fileopen.addChoosableFileFilter(filter);fileopen.setAcceptAllFileFilterUsed(false);fileopen.setFileFilter(filter);fileopen.setFileSelectionMode(JFileChooser.FILES_ONLY);int ret=fileopen.showOpenDialog(this);if(ret==JFileChooser.APPROVE_OPTION){File file=fileopen.getSelectedFile();System.out.println(file);driverPathTextField.setText(file.getPath());}}else if("OK".contains(e.getActionCommand())){System.setProperty("web.ie.driver",driverPathTextField.getText());dispose();}}}
basePanel.setSize(parent.getSize()); basePanel.setLayout(new BorderLayout()); if (useSlideButton) { final JPanel statusPanel = new JPanel(); basePanel.add(statusPanel, BorderLayout.SOUTH); statusPanel.add(new JButton("Slide Left") { private static final long serialVersionUID = 9204819004142223529L; statusPanel.add(new JButton("Slide Right") { basePanel.add(component); component.setSize(basePanel.getSize()); component.setLocation(0, 0);
public static void showCenterScreen(Window window, Component parent) { if (parent == null) { showCenterScreen(window); } else { Dimension sSize = parent.getSize(); Dimension wSize = window.getSize(); Point p = parent.getLocationOnScreen(); window.setLocation(p.x + ((sSize.width - wSize.width) / 2), p.y + ((sSize.height - wSize.height) / 2)); window.setVisible(true); } }
private JFrame frame = new JFrame("sssssssss"); private JButton tip1Null = new JButton(" test button "); 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); paintText(g, b, b.getBounds(), b.getText()); g.setColor(Color.red.brighter()); g.fillRect(0, 0, b.getSize().width, b.getSize().height);
import java.awt.*; import javax.swing.*; public class FrameInfo { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle bounds = env.getMaximumWindowBounds(); System.out.println("Screen Bounds: " + bounds ); GraphicsDevice screen = env.getDefaultScreenDevice(); GraphicsConfiguration config = screen.getDefaultConfiguration(); System.out.println("Screen Size : " + config.getBounds()); JFrame frame = new JFrame("Frame Info"); System.out.println("Frame Insets : " + frame.getInsets() ); frame.setSize(200, 200); System.out.println("Frame Insets : " + frame.getInsets() ); frame.setVisible( true ); System.out.println("Frame Size : " + frame.getSize() ); System.out.println("Frame Insets : " + frame.getInsets() ); System.out.println("Content Size : " + frame.getContentPane().getSize() ); } }
/** * Creates the pane. **/ private void createPane() { // This is for the OK, Cancel, and Apply buttons JPanel buttonPanel = getButtonPanel(); // This is for the rest JPanel panel = getModifyTab(); Container container = getContentPane(); container.setLayout(new BorderLayout()); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab(modifyActionTab_, panel); // container.add("West", new JPanel()); // container.add("East", new JPanel()); container.add("South", buttonPanel); container.add("Center", tabbedPane); setLocation(parent_.getLocation().x+parent_.getSize().width/2, parent_.getLocation().y+parent_.getSize().height/2); list_.setSelectedIndex(0); textField_.setText((String)list_.getSelectedValue()); }
JFrame frame = new JFrame(); frame.setSize(460, 500); frame.setTitle("Circles generator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.add(component); frame.getContentPane().validate(); frame.getContentPane().repaint(); public void paintComponent(Graphics g) { int margin = 10; Dimension dim = getSize(); super.paintComponent(g); g.setColor(Color.red);
import java.awt.*; import javax.swing.*; public class FrameInfo { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle bounds = env.getMaximumWindowBounds(); System.out.println("Screen Bounds: " + bounds ); GraphicsDevice screen = env.getDefaultScreenDevice(); GraphicsConfiguration config = screen.getDefaultConfiguration(); System.out.println("Screen Size : " + config.getBounds()); System.out.println(Toolkit.getDefaultToolkit().getScreenSize()); JFrame frame = new JFrame("Frame Info"); frame.setSize(200, 200); frame.setVisible( true ); System.out.println("Frame Size : " + frame.getSize() ); System.out.println("Frame Insets : " + frame.getInsets() ); System.out.println("Content Size : " + frame.getContentPane().getSize() ); } }
public void actionPerformed(ActionEvent e) { HistoryLogPane.setLayout(new ScrollPaneLayout());//<<< history = (JPanel)HistoryLogPane.getViewport().getView();//<<< javax.swing.JScrollPane HistoryLogPane = new javax.swing.JScrollPane(); HistoryLogPane.setLayout(new ScrollPaneLayout()); // edit @kiheru HistoryLogPane.setBackground(Color.WHITE); HistoryLogPane.setBorder(BorderFactory.createEmptyBorder()); HistoryLogPane.setLocation(0, 0); HistoryLogPane.add(history); HistoryLogPane.setSize(new Dimension(history.getSize())); history.setPreferredSize(history.getSize());//and we tried: history.setPreferredSize(history.getPreferredSize()); history.revalidate();//<<< System.out.println("SIZE: " + HistoryLogPane.getSize()); HistoryLocationPanel.removeAll(); HistoryLocationPanel.add(HistoryLogPane); HistoryLocationPanel.repaint(); HistoryLogPane.repaint(); }
JToolBar tools = new JToolBar(); tools.setFloatable(false); gui.add(tools, BorderLayout.PAGE_START); tools.add(new JButton("New")); // TODO - add functionality! tools.add(new JButton("Save")); // TODO - add functionality! tools.add(new JButton("Restore")); // TODO - add functionality! tools.addSeparator(); new ChessBoardWithColumnsAndRows(); JFrame f = new JFrame("ChessChamp"); f.add(cb.getGui()); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setLocationByPlatform(true); f.pack(); f.setMinimumSize(f.getSize()); f.setVisible(true);
System.out.println(Toolkit.getDefaultToolkit().getScreenSize()); JFrame frame = new JFrame("Frame Info"); frame.setVisible( true ); System.out.println("Frame Size : " + frame.getSize() ); System.out.println("Frame Insets : " + frame.getInsets() ); System.out.println("Content Size : " + frame.getContentPane().getSize() );
public void componentResized(ComponentEvent e) { JButton btn = (JButton) e.getComponent(); Dimension size = btn.getSize(); Insets insets = btn.getInsets(); size.width -= insets.left + insets.right; JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(btn); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (IOException exp) { exp.printStackTrace();
background.setBackground(ImageIO.read(new File("/path/to/your/image/on/your/disk"))); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(background); frame.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; frame.add(new JLabel("Hello world"), gbc); frame.add(new JLabel("I'm on top"), gbc); frame.add(new JButton("Clickity-clackity"), gbc); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (IOException exp) { exp.printStackTrace(); super.invalidate(); if (getWidth() > img.getWidth() || getHeight() > img.getHeight()) { scaled = getScaledInstanceToFill(img, getSize()); } else { scaled = img;
g.setColor(getBackground()); g.fillOval(0, 0, getSize().width-1, getSize().height-1); g.drawOval(0, 0, getSize().width-1, getSize().height-1); JFrame frame = new JFrame(); frame.getContentPane().setBackground(Color.yellow); frame.getContentPane().add(button); frame.getContentPane().setLayout(new FlowLayout()); frame.setSize(150, 150); frame.setVisible(true);
Floor2 mainPanel = new Floor2(w, h, gridDiv); JFrame frame = new JFrame("Floor2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); for (int i = 1; i < grid; i++) { int x = i * divisionSize; g2.drawLine(x, 0, x, getSize().height); g2.drawLine(0, y, getSize().width, y);
ui.add(text, BorderLayout.PAGE_START); io = new JTextField(15); Font font = io.getFont(); io.setHorizontalAlignment(SwingConstants.TRAILING); io.setFocusable(false); text.add(io); ui.add(buttons, BorderLayout.CENTER); String[] keyValues = { "7", "8", "9", "/", public void run() { ScriptEngineCalculator sc = new ScriptEngineCalculator(); JFrame f = new JFrame("Calculet"); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setContentPane(sc.getUI()); f.pack(); f.setMinimumSize(f.getSize()); f.setLocationByPlatform(true); f.setVisible(true);