使用 Nimbus 为 JButton 提供圆角

编程入门 行业动态 更新时间:2024-10-24 14:25:40
本文介绍了使用 Nimbus 为 JButton 提供圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用图像创建了一个按钮:

I have created a button using an image:

ImageIcon water = new ImageIcon("button.jpeg"); JButton exceptionButton = new JButton(water);

我希望按钮具有圆角,但我无法做到这一点.我听说过 Nimbus.我应该如何使用它为带或不带图像的按钮获得圆角?

I want the button to have rounded corners but I am not able to do that. I have heard about Nimbus. HOw should I use it to get rounded corners for the button with or without image?

推荐答案

试试这个类,这是使用 netbeans 的一种简单方法.

Try this class, it's an easy way using netbeans.

/* * NewJFrame.java * * Created on 3 mars 2014, 10:08 */ package GUI; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; /** * * @author boussarhane */ public class NewJFrame extends javax.swing.JFrame { protected UIManager.LookAndFeelInfo[] m_infos; String[] LAFNames; /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); m_infos = UIManager.getInstalledLookAndFeels(); LAFNames = new String[m_infos.length]; for (int i = 0; i < m_infos.length; i++) { //LAFNames[i] = m_infos[i].getName(); if (m_infos[i].getName().equals("Nimbus")) { try { UIManager.setLookAndFeel(m_infos[i].getClassName()); SwingUtilities.updateComponentTreeUI(this); } catch (ClassNotFoundException ex) { Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex); } } } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton2.setText("jButton2"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(jButton2)) .addContainerGap(315, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(41, 41, 41) .addComponent(jButton1) .addGap(18, 18, 18) .addComponent(jButton2) .addContainerGap(195, Short.MAX_VALUE)) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; // End of variables declaration }

更多推荐

使用 Nimbus 为 JButton 提供圆角

本文发布于:2023-11-25 12:10:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆角   Nimbus   JButton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!