gpt4 book ai didi

java - 有没有办法让我的地址栏的 JTextField 更大更 flex

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:10 25 4
gpt4 key购买 nike

我正在制作一个浏览器只是为了练习我的 Java 技能,有没有办法让我的地址栏(一个 JTextField)比 swing 的默认值更大,而且更 flex 。这是我的代码。

//imports of the GUI 
//import java.awt.*;
//import java.awt.event.*;
//import javax.swing.*;
//import javax.swing.event.*;
//import javax.swing.text.*;
//import javax.swing.GroupLayout.*;

//extends is to use the GUI class
public class ReadFile extends JFrame {
private JTextField addressBar; //to have the address bar
private JEditorPane display; //display the html information
//constructor

//Set the frame icon to an image loaded from a file.
public ReadFile() {
super("SPHERE"); //name of the browser


addressBar = new JTextField("enter an URL", 50); //inside the URL
addressBar.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
loadCrap(event.getActionCommand());
}
}

);
add(addressBar, BorderLayout.NORTH);



display = new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(
new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent event){
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
loadCrap(event.getURL().toString());

}
}
}
);
add(new JScrollPane(display), BorderLayout.CENTER);
setSize(600,200);
setVisible(true);

}
//load crap to display on the screen
private void loadCrap(String userText){
try{display.setPage(userText);
addressBar.setText(userText);}catch(Exception e){System.out.println("crap!")}
}



}

我想制作一个真正可用的浏览器,就像我想要显示 html 及其 CSS 页面一样,我还需要学习什么才能使它工作。

最佳答案

几乎所有这些都归结为操纵边界,但这可能不会产生您之后的结果,例如...

BigField

JTextField field = new JTextField(10);
field.setBorder(new CompoundBorder(field.getBorder(), new EmptyBorder(10, 0, 10, 0)));

创建圆 Angular 边框比较困难...

and also curvier

有几种方法可以实现,例如,您可以创建自己的 Border,例如...

public class RoundedBorder extends AbstractBorder {

@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = 5;
insets.right = 5;
insets.top = 5;
insets.bottom = 5;

return insets;
}

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g.create();
RoundRectangle2D shape = new RoundRectangle2D.Float(0, 0, width - 1, height - 1, 20, 20);
g2d.setColor(Color.BLACK);
g2d.draw(shape);
g2d.dispose();
}

}

然后将它应用到你的领域......

field.setBorder(new CompoundBorder(new RoundedBorder(), new EmptyBorder(10, 0, 10, 0)));

这会产生类似...

enter image description here Not Transparent

但我不喜欢这样,因为如果你仔细观察,边界外的区域仍然被绘制......你可以让边界填充这个区域,但我喜欢能够为组件提供透明功能,所以相反,你可以伪造它......

Fake Border Transparent field

基本上,它所做的是创建一个自定义组件,可以绘制周围的字段,但是,因为它可以更好地控制绘制过程,还可以提供边框外的透明效果...

public class FakeRoundedBorder extends JPanel {

private JTextField field;

public FakeRoundedBorder(JTextField field) {
this.field = field;
setBorder(new EmptyBorder(5, 5, 5, 5));
field.setBorder(new EmptyBorder(10, 0, 10, 0));
setLayout(new BorderLayout());
add(field);
setOpaque(false);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
RoundRectangle2D shape = new RoundRectangle2D.Float(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
g2d.setColor(field.getBackground());
g2d.fill(shape);
g2d.setColor(Color.BLACK);
g2d.draw(shape);
g2d.dispose();
}

}

当然,这只是一堆示例,您需要清理它并自己提供对值的自定义 ;)

关于java - 有没有办法让我的地址栏的 JTextField 更大更 flex ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com