找到关于 Java 8 的 4330 篇文章

如何在 Java 中为 JButton 设置 action command

Krantik Chavan
更新于 2019-07-30 22:30:26

4K+ 次浏览

通过设置 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 设置 action command 的示例:示例 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 ... 阅读更多

如何在 Java Swing 中更改按钮边框

Krantik Chavan
更新于 2019-07-30 22:30:26

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); ... 阅读更多

如何在 Java 中为 JButton 添加 action listener

Krantik Chavan
更新于 2019-07-30 22:30:26

8K+ 次浏览

下面是为按钮添加 action listener 的示例:示例 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() { ... 阅读更多

我们可以在 Java 中获取支持的图像类型吗?

Krantik Chavan
更新于 2019-07-30 22:30:26

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

在 Swing 中使用柔和斜面边框的 Java 程序

Daniol Thomas
更新于 2019-07-30 22:30:26

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 ... 阅读更多

如何在 Java Swing 中使用 EmptyBorder 留出空间

Daniol Thomas
更新于 2019-07-30 22:30:26

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)); ... 阅读更多

在 Swing 中创建圆角边框的 Java 程序

Krantik Chavan
更新于 2019-07-30 22:30:26

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)); ... 阅读更多

在 Swing 中为面板添加标题边框的 Java 程序

Krantik Chavan
更新于 2019-07-30 22:30:26

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); } } 输出 阅读更多

如何为 Java Swing 应用程序设置全屏模式?

Krantik Chavan
更新于 2019-07-30 22:30:26

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);     ... 阅读更多

当用户将鼠标悬停在 Java Swing JDialog 中的某些文本上时,显示自定义光标

Krantik Chavan
更新于 2019-07-30 22:30:26

浏览量: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));   ... 阅读更多

广告