我必須創建一個具有精確顏色的圓形按鈕。
我做了很多研究才能做到這一點,我快到了!
我選擇使用圓形邊框,因為否則對我來說似乎不可能:/(我是 Java 新手)。
所以我只需要找到一種方法來將按鈕(文本)內容的背景設定為正確的顏色,我就完成了。(我目前只有邊框并禁用背景以查看圓形部分,因此文本的背景為空......)
結果 :

預期結果 :

我已經嘗試過論文:
import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagLayout; import java.awt.RenderingHints; import java.awt.geom.RoundRectangle2D; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setBorder(new EmptyBorder(32, 32, 32, 32)); setLayout(new GridBagLayout()); add(new Button("This is a test")); } } public class BrandColors { public static final Color TEXT_ON_SECOUNDARY = Color.WHITE; public static final Color SECOUNDARY = Color.RED; } public class Button extends JButton { private int xPadding = 10; public Button(String text) { super(text); this.init(); } private void init() { this.setFont(new Font("Arial", Font.PLAIN, 16)); this.setForeground(BrandColors.TEXT_ON_SECOUNDARY); this.setContentAreaFilled(false); this.setBorderPainted(false); this.setBackground(BrandColors.SECOUNDARY); this.setOpaque(false); } @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); RenderingHints hints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); g2d.setRenderingHints(hints); g2d.setColor(getBackground()); g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1, 15, 15)); g2d.setColor(getForeground()); super.paintComponent(g2d); g2d.dispose(); } } }現在,這里的技巧是知道這
paintComponent也將呈現文本,所以我們需要在呼叫之前繪制背景super.paintComponentUI 委托示例...
現在,Swing 的特性之一是它的“可插拔外觀和感覺”。這允許您修改組件的“外觀和感覺”,而無需修改其余代碼。
以下示例顯示了一種為特定實體設定 UI 委托的方法
JButton
import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.RenderingHints; import java.awt.geom.RoundRectangle2D; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.plaf.basic.BasicButtonUI; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setBorder(new EmptyBorder(32, 32, 32, 32)); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(4, 4, 4, 4); JButton button = new JButton("This is a normal button"); add(button, gbc); JButton superButton = new JButton("This is a super button"); superButton.setUI(new RoundedButtonUI()); add(superButton, gbc); } } public class BrandColors { public static final Color TEXT_ON_SECOUNDARY = Color.WHITE; public static final Color SECOUNDARY = Color.RED; } public class RoundedButtonUI extends BasicButtonUI { @Override protected void installDefaults(AbstractButton b) { super.installDefaults(b); b.setOpaque(false); b.setBackground(BrandColors.SECOUNDARY); b.setForeground(BrandColors.TEXT_ON_SECOUNDARY); } @Override public void paint(Graphics g, JComponent c) { Graphics2D g2d = (Graphics2D) g.create(); RenderingHints hints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); g2d.setRenderingHints(hints); g2d.setColor(c.getBackground()); g2d.fill(new RoundRectangle2D.Double(0, 0, c.getWidth() - 1, c.getHeight() - 1, 15, 15)); g2d.setColor(c.getForeground()); super.paint(g, c); g2d.dispose(); } } }影響 UI 中的所有按鈕
如果要更改 UI 中的所有按鈕,而無需更改任何相關代碼,則可以將 UI 委托設定為所有按鈕使用的默認 UI 委托
為此,我必須進行一些額外的更改。首先,委托類需要在它自己的檔案中(請注意包名)并且我必須實作該
static方法createUIpackage stackoverflow; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.RoundRectangle2D; import javax.swing.AbstractButton; import javax.swing.JComponent; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicButtonUI; public class RoundedButtonUI extends BasicButtonUI { private static RoundedButtonUI shared; public static ComponentUI createUI(JComponent c) { if (shared != null) { return shared; } shared = new RoundedButtonUI(); return shared; } @Override protected void installDefaults(AbstractButton b) { super.installDefaults(b); b.setOpaque(false); b.setBackground(BrandColors.SECOUNDARY); b.setForeground(BrandColors.TEXT_ON_SECOUNDARY); } @Override public void paint(Graphics g, JComponent c) { Graphics2D g2d = (Graphics2D) g.create(); RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints(hints); g2d.setColor(c.getBackground()); g2d.fill(new RoundRectangle2D.Double(0, 0, c.getWidth() - 1, c.getHeight() - 1, 15, 15)); g2d.setColor(c.getForeground()); super.paint(g, c); g2d.dispose(); } }現在,在你做任何其他事情之前,我需要安裝它,
UIManager.getDefaults().put(new JButton().getUIClassID(), "stackoverflow.RoundedButtonUI");. 這應該在您呼叫任何其他 UI 相關代碼之前完成(并且在您設定外觀之后,如果您正在這樣做)然后我就可以正常運行代碼了

import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { UIManager.getDefaults().put(new JButton().getUIClassID(), "stackoverflow.RoundedButtonUI"); JFrame frame = new JFrame(); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setBorder(new EmptyBorder(32, 32, 32, 32)); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(4, 4, 4, 4); JButton button = new JButton("This is a normal button"); add(button, gbc); JButton superButton = new JButton("This is a super button"); add(superButton, gbc); } } }請注意
為了以這種方式安裝新的 UI 委托,您必須提供完全限定的類名,即完整的包路徑和類名。
在我上面的例子中,我使用
stackoverflow作為我的包名(我很懶),所以安裝看起來像UIManager.getDefaults().put(new JButton().getUIClassID(), "stackoverflow.RoundedButtonUI");
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401897.html
