问题描述
左侧明显较厚.使用无光泽边框不会产生这种情况.我想知道的是为什么LineBorder会创建这个而不是MatteBorder.
Noticeably thicker on the left side. Using a Matte Border does not produce this. What I want to know is why LineBorder would create this and not MatteBorder.
这是一个放置在JPanel中的JButton,然后将其添加到JFrame中.
This is a JButton being placed in a JPanel which is then being added to a JFrame.
相关代码:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class View extends JPanel{
public View(){
JPanel displayPanel = new JPanel();
displayPanel.setBackground(Color.WHITE);
displayPanel.setSize(500, 500);
JButton backspace = createButton("\u2190", "backspace", Color.RED, null, new Controller());
backspace.setOpaque(false);
backspace.setSize(25, 25);
backspace.setBorder(new LineBorder(Color.RED, 1));
backspace.setMinimumSize(new Dimension(25, 25));
backspace.setPreferredSize(new Dimension(25, 25));
displayPanel.add(backspace);
add(displayPanel);
}
private JButton createButton (String text, String ac, Color fg, Color bg, ActionListener handler){
JButton button = new JButton();
button.setText(text);
button.setForeground(fg);
button.setBackground(bg);
if (ac!= null){
button.setActionCommand(ac);
}
button.setFont(new Font(button.getFont().getName(), button.getFont().getStyle(), 20));
button.addActionListener(handler);
return button;
}
private class Controller implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String foo = e.getSource().toString();
System.out.println(foo);
//display.setText(button.getActionListeners().toString());
}
}
}
主要:
package test;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Foo{
public static void main (String args[]){
final int height = 400;
final int width = 330;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame = new JFrame();
frame.setSize(330, 400);
frame.setLocation((screen.width - width )/2, (screen.height - height)/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setContentPane(new View());
frame.setVisible(true);
}
});
}
}
如果我将其切换为
backspace.setBorder(new MatteBorder(1, 1, 1, 1, Color.RED));
根据需要显示.
是什么导致LineBorder中的厚度不一致?
What is causing the thickness inconsistency in LineBorder?
推荐答案
在百灵鸟上,决定从我的构建路径中删除1.8,然后重新添加相同的库.按钮现在可以正常绘制了.
On a lark, decided to remove 1.8 from my buildpath and then re-add the same Library back. Button draws fine now.
这是在Eclipse Luna中,众所周知,它具有一些buildpath怪癖(例如,除非您也执行上述技巧,否则JavaFX无法正确导入.)
This is in Eclipse Luna, which is known to have some buildpath quirks (such as JavaFX not importing properly unless you do the above trick as well. )
这篇关于一侧使LineBorder变厚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!