找到 4330 篇文章 关于 Java 8
971 次浏览
要为 JFrame 设置默认按钮,请使用 setDefaultButton() 方法 -JFrame frame = new JFrame(); frame.getRootPane().setDefaultButton(button);以下是如何为 JFrame 设置默认按钮的示例 -示例import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo { public static void main(String args[]) { JButton button = new JButton("Demo Button!"); JFrame frame = new JFrame(); frame.setSize(500, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getRootPane().setDefaultButton(button); button.setMnemonic(KeyEvent.VK_A); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { ... 阅读更多
2K+ 次浏览
使用 setMinimumSize() 方法为 JFrame 设置最小尺寸限制 -JFrame frame = new JFrame(); frame.setMinimumSize(new Dimension(500, 300));以下是如何为 JFrame 设置最小尺寸限制的示例 -示例import java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Close!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(button); button.addActionListener(e -> { frame.dispose(); }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setMinimumSize(new Dimension(500, ... 阅读更多
2K+ 次浏览
要获取 JFrame 窗口大小信息,您可以使用以下方法 -environment.getMaximumWindowBounds();对于屏幕尺寸 -config.getBounds()对于框架尺寸 -frame.getSize());以下是如何获取 JFrame 窗口大小信息的示例 -示例import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import javax.swing.JFrame; public class SwingDemo { public static void main(String[] args) { GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle bounds = environment.getMaximumWindowBounds(); System.out.println("Screen Bounds = " + bounds); GraphicsDevice device = environment.getDefaultScreenDevice(); GraphicsConfiguration config = device.getDefaultConfiguration(); System.out.println("Screen Size = " + config.getBounds()); JFrame frame ... 阅读更多
546 次浏览
要清除所有选择,请在 Java 中使用 List clearSelection() 方法 -JList list = new JList(sports); list.clearSelection();上面,Sports 数组中的元素是一个字符串数组 -String sports[]= { "Cricket", "Football", "Hockey", "Rugby"};以下是如何在 JList 中清除所有选择的示例 -示例package my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); SwingDemo s = new SwingDemo(); JPanel panel = new JPanel(); String sports[]= ... 阅读更多
315 次浏览
要选择第二个索引,请使用 setSelectedIndex() 方法 -JList new JList(sports); list.setSelectedIndex(2);以下是如何在 Java JList 中选择第二个索引的示例 -示例package my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); SwingDemo s = new SwingDemo(); JPanel panel = new JPanel(); String sports[]= { "Cricket","Football","Hockey","Rugby"}; list = new JList(sports); list.setSelectedIndex(2); panel.add(list); frame.add(panel); frame.setSize(400,400); frame.setVisible(true); } }输出
103 次浏览
以下是如何从文本创建 JRadioButton 的示例 -示例package my; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class SwingDemo { public static void main(String[] args) { JRadioButton radio1 = new JRadioButton("Cricket"); JRadioButton radio2 = new JRadioButton("Football"); ButtonGroup group = new ButtonGroup(); group.add(radio1); group.add(radio2); radio1.setSelected(true); JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Fav Sports:")); frame.add(radio1); ... 阅读更多
14K+ 次浏览
要向 JFrame 添加背景图像,请使用 Image 类的 getImage() 方法 -Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");现在,绘制图像 -public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, null); }以下是如何向 JFrame 添加背景图像的示例 -示例import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.io.IOException; import javax.swing.JPanel; public class SwingDemo extends javax.swing.JFrame { Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg"); public SwingDemo() throws IOException { this.setContentPane(new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); ... 阅读更多
600 次浏览
要创建没有边框和标题栏的 JFrame,请使用 setUndecorated() -JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(400, 300)); frame.setUndecorated(true);以下是如何创建没有边框和标题栏的 JFrame 的示例 -示例import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(400, 300)); frame.setUndecorated(true); JPanel panel = new JPanel(); panel.add(new JLabel("Demo!")); panel.add(new JButton(new AbstractAction("Close") { ... 阅读更多
444 次浏览
这里,我们正在激活一个框架。对于停用,我们使用了 dispose -Thread.sleep(2000); frame.setVisible(false);由于我们将 sleep 设置为 2000 毫秒,因此框架首先激活,然后在 2 秒后停用。以下是如何激活和停用 JFrame 的示例 -示例import java.awt.Frame; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo { public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame(); frame.add(new JLabel("Demo")); frame.pack(); frame.setVisible(true); Thread.sleep(2000); frame.setState(Frame.ICONIFIED); Thread.sleep(2000); frame.setVisible(false); frame.dispose(); ... 阅读更多
169 次浏览
要从 JFileChooser 获取目录,请使用 setFileSelectionMode 模式 −JFileChooser file = new JFileChooser(); file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);以下是如何从 JFileChooser 获取目录的示例 −示例import javax.swing.JFileChooser; public class SwingDemo { public static void main(String[] args) { JFileChooser file = new JFileChooser(); file.setMultiSelectionEnabled(false); file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int res = file.showOpenDialog(null); if (res == JFileChooser.APPROVE_OPTION) { java.io.File f = file.getSelectedFile(); System.err.println(f.getPath()); } } }输出