找到关于 Java 8 的4330 篇文章
4K+ 次浏览
使用 set action command,我们在这里展示了单击按钮时在控制台中显示的消息。首先设置按钮:JButton btn = new JButton("Demo Button");现在,设置 Action Listener 以在单击按钮时触发:ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { String str = event.getActionCommand(); System.out.println("Clicked = " + str); } };以下是如何在 JButton 中设置操作命令的示例:示例import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public class SwingDemo { public static void main(final String args[]) { JButton btn ... 阅读更多
982 次浏览
对于按钮边框,在 Java 中使用 createLineBorder() 方法,该方法允许您设置边框的颜色:JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE);以下是如何在 Java 中更改按钮边框的示例:示例import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Button Border"); Container container = frame.getContentPane(); JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE); button.setBorder(border); ... 阅读更多
8K+ 次浏览
以下是为按钮添加动作监听器的示例:示例package my; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame frame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingDemo(){ prepareGUI(); } public static void main(String[] args){ SwingDemo swingControlDemo = new SwingDemo(); swingControlDemo.showButtonDemo(); } private void prepareGUI(){ frame = new JFrame("Java Swing"); frame.setSize(500, 500); frame.setLayout(new GridLayout(3, 1)); frame.addWindowListener(new WindowAdapter() { ... 阅读更多
87 次浏览
是的,我们可以使用 Java 中的 ImageIO 类获取支持的图像类型。以下是获取 Java 中支持的图像类型的示例:示例package my; import javax.imageio.ImageIO; public class SwingDemo { public static void main(String[] args) throws Exception { String[] imgTypes = ImageIO.getReaderFileSuffixes(); System.out.print("Supported Image Types = "); for (String type : imgTypes) { System.out.print("" + type); } } }输出Supported Image Types = jpg tif tiff bmp gif png wbmp jpeg
206 次浏览
在这里,我们在 JComboBox 上创建柔和斜面边框:JComboBox comboBox = new JComboBox(list);现在,设置斜面边框:comboBox.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));以下是在 Swing 中使用柔和斜面边框的示例:示例import java.awt.Font; import java.awt.GridBagLayout; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.BevelBorder; import javax.swing.border.SoftBevelBorder; public class SwingDemo { static final String list[] = { "One", "Two", "Three", "Four", "Five", "Six" }; public static void main(String[] args) { JFrame window = new JFrame("ComboBox Example"); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); JComboBox comboBox = new JComboBox(list); comboBox.setBorder(new ... 阅读更多
460 次浏览
让我们首先创建一个 JPanel 并设置标题边框:JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));现在创建 Empty Border:JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));以下是如何在 Java Swing 中使用 EmptyBorder 留出空间的示例:示例import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel")); JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100)); ... 阅读更多
5K+ 次浏览
让我们首先创建一个框架:JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true);现在,创建圆角边框:double x = 50; double y = 50; frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));以下是在 Swing 中创建圆角边框的示例:示例import java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true); double x = 50; double y = 50; frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50)); ... 阅读更多
195 次浏览
要将标题边框设置为面板,让我们首先为 Java Swing 应用程序创建一个面板:JPanel panel = new Jpanel(new BorderLayout());现在,设置标题边框:panel.setBorder(new TitledBorder("Displaying Titled Border"));以下是在 Swing 中向面板添加标题边框的示例:示例import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(new TitledBorder("Displaying Titled Border")); panel.add(new JButton("Demo Button"), BorderLayout.SOUTH); JOptionPane.showMessageDialog(null, panel); } }输出阅读更多
5K+ 次浏览
要为您的 Java Swing 应用程序设置全屏模式,请使用 setFullScreenWindow() 方法:GraphicsDevice device = graphics.getDefaultScreenDevice(); JFrame frame = new JFrame("Fullscreen"); device.setFullScreenWindow(frame);以下是如何为 Java Swing 应用程序设置全屏模式的示例:示例import java.awt.Color; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { GraphicsEnvironment graphics = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = graphics.getDefaultScreenDevice(); JFrame frame = new JFrame("Fullscreen"); JPanel panel = new JPanel(); JLabel label = new JLabel("", JLabel.CENTER); ... 阅读更多
575 次浏览
首先,设置要在其上显示自定义光标的标签:JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");现在,将光标设置为显示为手形光标,而不是默认光标:label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));以下是如何在用户将鼠标移到某些文本上时显示自定义光标的示例:示例import java.awt.Cursor; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class SwingDemo extends JFrame { private void ShowDialog() { JLabel label = new JLabel("Demo text! Hand cursor is visible on hover..."); label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); ... 阅读更多