Is there a way to make JTextField for my address bar larger and curvier

孤人 提交于 2019-12-02 19:07:54

问题


I'm making a browser just to practice my Java skills, is there a way to make my address bar which is a JTextField, larger instead of the swing's default value and also curvier. Here's my code.

//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!")}
    }



    } 

I want to make a really usable browser, like I want the html and its' CSS pages to show, what else do I have to learn to make this work.


回答1:


Almost all of this comes down to manipulating the border, but this may not produce the results your after, for example...

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

Creating a rounded border is more difficult...

and also curvier

There are a few ways you might achieve, this for example, you could create a Border of your own, for example...

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();
    }

}

Then apply it to your field...

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

Which produces something like...

But I don't like this, as, if you look closely, the area outside the border is still painted...You could have the border fill this area, but I like having the ability to provide transparent capabilities to components, so instead, you could fake it...

Basically, what this does is creates a custom component that can paint the around the field, but, because it can better control the painting process, can also provide transparency outside the border effect...

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();
    }

}

This is just a bunch of examples of course, you'll need to clean it up and provide customisation to the values yourself ;)




回答2:


I'm not sure what you mean by "curvier". But here's a way to resize it and set the font:

    addressBar.setFont(new Font("TimesRoman", Font.ITALIC, 30));


来源:https://stackoverflow.com/questions/23945781/is-there-a-way-to-make-jtextfield-for-my-address-bar-larger-and-curvier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!