E ai gente, tava fazendo um chatzinho, e queria que antes de iniciar o programa aparecesse um inputdialog para o usuario escrever seu nick, para ai posteriormente eu pegar o nick que o usuario digito e concatenar antes de suas mensagens e tals, alguem me ajuda a fazer essa alteração simples no codigo ?? (deixei um comentario para localizarem )
[code]public class Chat {
private JFrame frame;
private final JTextPane textPane = new JTextPane();
private final JTextField textField = new JTextField();
private final JScrollPane scrollPane = new JScrollPane();
EntradaChat apelido = new EntradaChat();
public static void main(String[] args) {
//SHOWINPUTDIALOG AQUI, GRAVANDO O APELIDO DIGITADO EM UMA VARIAVEL APELIDO
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Chat window = new Chat();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Chat() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.inactiveCaptionText);
frame.setBounds(100, 100, 494, 365);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
scrollPane.setBounds(10, 31, 458, 246);
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(textPane);
textPane.setBackground(SystemColor.inactiveCaption);
textPane.setEditable(false);
textField.setBounds(10, 288, 321, 28);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.addActionListener(new InsereTexto());
JButton btnEnviar = new JButton("Enviar");
btnEnviar.setBackground(SystemColor.inactiveCaptionText);
btnEnviar.addActionListener(new InsereTexto());
btnEnviar.setBounds(348, 287, 110, 30);
frame.getContentPane().add(btnEnviar);
}
class InsereTexto implements ActionListener {
//A CONCATENAÇÃO SERA FEITA ABAIXO
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido + textField.getText());
textField.setText("");
}
}
}[/code]
Boa noite sergiorj…
private JFrame frame;
private final JTextPane textPane = new JTextPane();
private final JTextField textField = new JTextField();
private final JScrollPane scrollPane = new JScrollPane();
static String apelido = "";
public static void main(String[] args) {
//SHOWINPUTDIALOG AQUI, GRAVANDO O APELIDO DIGITADO EM UMA VARIAVEL APELIDO
apelido = JOptionPane.showInputDialog(null, "Digite o Nick", "Login", JOptionPane.QUESTION_MESSAGE);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Combo window = new Combo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Combo() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.inactiveCaptionText);
frame.setBounds(100, 100, 494, 365);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
scrollPane.setBounds(10, 31, 458, 246);
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(textPane);
textPane.setBackground(SystemColor.inactiveCaption);
textPane.setEditable(false);
textField.setBounds(10, 288, 321, 28);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.addActionListener(new InsereTexto());
JButton btnEnviar = new JButton("Enviar");
btnEnviar.setBackground(SystemColor.inactiveCaptionText);
btnEnviar.addActionListener(new InsereTexto());
btnEnviar.setBounds(348, 287, 110, 30);
frame.getContentPane().add(btnEnviar);
}
class InsereTexto implements ActionListener {
//A CONCATENAÇÃO SERA FEITA ABAIXO
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido + textField.getText());
textField.setText("");
}
}
Espero ter te ajudado, mas quando eu rodei o seu codigo, o apelido está ficando concatenado com o que foi digitado, você poderia deixar assim:
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido + " Diz: "+ "\n" + textField.getText());
textField.setText("");
}
[quote=AndreSorge]Boa noite sergiorj…
private JFrame frame;
private final JTextPane textPane = new JTextPane();
private final JTextField textField = new JTextField();
private final JScrollPane scrollPane = new JScrollPane();
static String apelido = "";
public static void main(String[] args) {
//SHOWINPUTDIALOG AQUI, GRAVANDO O APELIDO DIGITADO EM UMA VARIAVEL APELIDO
apelido = JOptionPane.showInputDialog(null, "Digite o Nick", "Login", JOptionPane.QUESTION_MESSAGE);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Combo window = new Combo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Combo() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.inactiveCaptionText);
frame.setBounds(100, 100, 494, 365);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
scrollPane.setBounds(10, 31, 458, 246);
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(textPane);
textPane.setBackground(SystemColor.inactiveCaption);
textPane.setEditable(false);
textField.setBounds(10, 288, 321, 28);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.addActionListener(new InsereTexto());
JButton btnEnviar = new JButton("Enviar");
btnEnviar.setBackground(SystemColor.inactiveCaptionText);
btnEnviar.addActionListener(new InsereTexto());
btnEnviar.setBounds(348, 287, 110, 30);
frame.getContentPane().add(btnEnviar);
}
class InsereTexto implements ActionListener {
//A CONCATENAÇÃO SERA FEITA ABAIXO
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido + textField.getText());
textField.setText("");
}
}
Espero ter te ajudado, mas quando eu rodei o seu codigo, o apelido está ficando concatenado com o que foi digitado, você poderia deixar assim:
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido + " Diz: "+ "\n" + textField.getText());
textField.setText("");
}
[/quote]
Muito obrigado

Aproveitando o espaço, eu tb to desenvolvendo uma aplicação parecida mas no meu caso eu queria botar uma cor na variavel apelido
como eu faço isso ??
Gabe Develop,
Qual o componente do apelido?
Se for um JTextField, faz o seguinte:
textfield.setForeground(Color.CYAN);
O exemplo acima altera a cor para CYAN.
Na maioria dos componentes o metódo para alterar a cor da fonte é o mesmo setForeground. Este também funciona com JLabel…
[quote=Victor_Leandro]Gabe Develop,
Qual o componente do apelido?
Se for um JTextField, faz o seguinte:
textfield.setForeground(Color.CYAN);
O exemplo acima altera a cor para CYAN.
Na maioria dos componentes o metódo para alterar a cor da fonte é o mesmo setForeground. Este também funciona com JLabel…[/quote]
static String apelido = "";
apelido = JOptionPane.showInputDialog(null, "Digite o Nick", "Login", JOptionPane.QUESTION_MESSAGE);
.
.
.
.
.
class InsereTexto implements ActionListener {
//A CONCATENAÇÃO SERA FEITA ABAIXO
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido + textField.getText());
textField.setText("");
}
}
Eu segui a idéia de apelido por esse tópico, não sei se é a melhor pratica mas fiz o mesmo esquema do código ali em cima, que pega o valor do InputDialog e armaneza em uma variavel String pra concatenar depois
neste caso, vc sabe como eu faço para mudar a cor apenas da variavel apelido ???
valeu
GabeDevelop, boa noite…
fiz uma gabizinha, mas nao sei se funciona…fiz de cabeca…testa ai e avisa se funfa…
private JFrame frame;
private final JTextPane textPane = new JTextPane();
private final JTextField textField = new JTextField();
private final JScrollPane scrollPane = new JScrollPane();
static JTextField apelido = new JTextField();
public static void main(String[] args) {
//SHOWINPUTDIALOG AQUI, GRAVANDO O APELIDO DIGITADO EM UMA VARIAVEL APELIDO
apelido.setText(JOptionPane.showInputDialog(null, "Digite o Nick", "Login", JOptionPane.QUESTION_MESSAGE));
apelido.setForeground(Color.RED);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Combo window = new Combo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Combo() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.inactiveCaptionText);
frame.setBounds(100, 100, 494, 365);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
scrollPane.setBounds(10, 31, 458, 246);
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(textPane);
textPane.setBackground(SystemColor.inactiveCaption);
textPane.setEditable(false);
textField.setBounds(10, 288, 321, 28);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.addActionListener(new InsereTexto());
JButton btnEnviar = new JButton("Enviar");
btnEnviar.setBackground(SystemColor.inactiveCaptionText);
btnEnviar.addActionListener(new InsereTexto());
btnEnviar.setBounds(348, 287, 110, 30);
frame.getContentPane().add(btnEnviar);
}
class InsereTexto implements ActionListener {
//A CONCATENAÇÃO SERA FEITA ABAIXO
public void actionPerformed(ActionEvent arg0) {
textPane.setText(textPane.getText() + "\n" + apelido.getText() + " Diz: "+ "\n" + textField.getText());
textField.setText("");
}
}
Abracos