import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.font.*;
import javax.swing.border.AbstractBorder.*;
import javax.swing.border.TitledBorder.*;
import javax.swing.border.*;
public class slss {
public slss() {
JFrame frame = new JFrame();
JPanel cp = (JPanel)frame.getContentPane();
cp.setLayout(null);
TitledBorder border1 = new TitledBorder( "Sports");
frame.add(border1);
frame.setSize(1280,775);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new slss();
}
}
Next time please post these questions the Swing forums
and check the online tutorials.
http://java.sun.com/docs/books/tutorial/uiswing/components/border.html
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.font.*;
import javax.swing.border.AbstractBorder.*;
import javax.swing.border.TitledBorder.*;
import javax.swing.border.*;
public class slss {
// http://java.sun.com/docs/books/tutorial/uiswing/components/border.html
public slss() {
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(null);
TitledBorder border1 = new TitledBorder( "Sports");
if(cp instanceof JComponent){
((JComponent)cp).setBorder(border1);
}
frame.setSize(1280,775);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new slss();
}
}
by the way, this is how you should REALLY be accomplishing this.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.font.*;
import javax.swing.border.AbstractBorder.*;
import javax.swing.border.TitledBorder.*;
import javax.swing.border.*;
public class SLSS{
public static void main(String[] args){
new SLSS();
}
public SLSS(){
border1 = new TitledBorder( "Sports");
panel = new JPanel();
panel.setBorder(border1);
frame = new JFrame();
frame.setContentPane(panel);
frame.setSize(1280,775);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
TitledBorder border1;
JPanel panel;
JFrame frame;
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.font.*;
import javax.swing.border.AbstractBorder.*;
import javax.swing.border.TitledBorder.*;
import javax.swing.border.*;
public class slss {
// http://java.sun.com/docs/books/tutorial/uiswing/components/border.html
public slss() {
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(null);
TitledBorder border1 = new TitledBorder( "2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007
2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 ");
if(cp instanceof JComponent){
((JComponent)cp).setBorder(border1);
}
frame.setSize(1280,775);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new slss();
}
}
First, dont use that code use the proper form I just posted.
Second, yes, lol.
Just make them separate strings and join them with +
"string 1" + "string 2" etc.
TitledBorder border1 = new TitledBorder( "2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007" +
"2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 ");
You can't break String literals onto multiple lines like that. If you want to split it onto multiple lines, make it into two literals concatenated with +:
String s = "This is a
multi-line String";// wrong
String s = "This is a " +
"multi-line String";// right
If you're trying to display a multile line title in the border, I don't know if that's possible, but "\n" is a newline character, you can try that.
String s = "This is a \nmulti-line String";
now a second issue proberly something easy...my titled border only fills the top not the sides and the bottom
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.font.*;
import javax.swing.border.AbstractBorder.*;
import javax.swing.border.TitledBorder.*;
import javax.swing.border.*;
public class slss {
// http://java.sun.com/docs/books/tutorial/uiswing/components/border.html
public slss() {
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(null);
TitledBorder border1 = new TitledBorder( "2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007"+
"2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007"+
"2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007"+
"2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007 2006-2007" );
if(cp instanceof JComponent){
((JComponent)cp).setBorder(border1);
}
frame.setSize(1280,775);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new slss();
}
}
> im not quite sure how to align the border to top
> bottom right left
You can't. There's only one border for each component. If you call setBorder a second time, you just replace the first border. There's no border that places text on more that one side of the component. I'm running out of ways to say this to you. You need to accept this.
im reading that site u gave me about titled border
Create a titled border. The string argument specifies the title to be displayed. The optional font and color arguments specify the font and color to be used for the title's text. The border argument specifies the border that should be displayed along with the title. If no border is specified, then a look-and-feel-specific default border is used.
By default, the title straddles the top of its companion border and is left-justified. The optional integer arguments specify the title's position and justification, in that order. TitledBorder defines these possible positions: ABOVE_TOP, TOP (the default), BELOW_TOP, ABOVE_BOTTOM, BOTTOM, and BELOW_BOTTOM. You can specify the justification as LEADING (the default), CENTER, or TRAILING. In locales with Western alphabets LEADING is equivalent to LEFT and TRAILING is equivalent to RIGHT.
and if u cant then i dont really care right about now....but heres another thing
1. when u place a picture into a label and paste it on ur cp, it doesnt compress or stretch that image to fit. so i wanted it at 100,100 when the picture is much bigger?
any idea on how to solve this one?
If you really want to use MatteBorder, (which is the closest you'll get without coding it yourself), create an image that has your text written on it. Use the ImageIcon constructor that takes a filename to load the image, then pass that to the MatteBorder constructor that takes an Icon. It'll be written horizontally all around, but it should be there.
Just because I was curious about if this would really work or not, I created a quick test, you can try it out and see if it works for you.
package sandbox;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(new javax.swing.border.MatteBorder(new javax.swing.ImageIcon("D:\\Profiles\\HDAVIS\\My Documents\\My Pictures\\test.PNG")));
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
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.JPanel jPanel1;
// End of variables declaration
}