jtextfieldからデータを取得する文字列があります。このデータを3つの文字列(1番目、2番目、3番目の文字列)で3文字に分割します例外が機能していません(JOptionPaneメッセージは表示されません)
try {
String text,first,second,third,result;
Swing get = new Swing();
text = get.getMyText();
first = text.substring(0,1);
second = text.substring(1,2);
third = text.substring(2,3);
result = first + third + second;
if(text.isEmpty) {
throw new Exception();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Empty","....",JOptionPane.ERROR_MESSAGE);
}
は私の代わりにJOptionPaneのメッセージのシステムからこの奇妙なメッセージが表示されます。
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
私がここで欠場何の任意の手掛かり?あなたがテキストかどうかをテストする必要が
public class Swing {
// second line of the frame
private static JFrame window; // creating the frame
private static JTextField text;
// setting the frame
/**
* @wbp.parser.entryPoint
*/
public void Run() {
window = new JFrame("Tool");
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(new Color(230, 230, 250));
window.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
window.getContentPane().isOpaque();
window.getContentPane().setLayout(null);
// the textfield
text = new JTextField();
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setForeground(Color.BLUE);
text.setFont(new Font("David", Font.PLAIN, 20));
text.setColumns(10);
text.setBounds(504, 11, 149, 20);
window.getContentPane().add(text);
// adding the button from the other class (MyBtn)
MyBtn addBTN = new MyBtn();
window.getContentPane().add(addBTN.run());
// setting the frame
window.setVisible(true);
window.setSize(750, 500);
window.setLocationRelativeTo(null);
}
// preparing the getters for the input
public String getText() {
return text.getText();
}
ヒント:あなたは私たちがあなたを助けるために私たちの時間を過ごしたいです。だからできるだけ簡単にするために時間を割いてください。これは、適切にフォーマットされたコード例から始まります。そして、副作用:あなたの変数名は役に立たない。彼らは、オブジェクトが本当に何であるかをあまり伝えません。最初、二番目、三番目...何?そして、「スイング」クラスについてはどうですか?その名前は読者だけを混乱させる!そして、2番のヒント:このような問題は、単にトレースステートメントを追加するだけで解決できることがよくあります。あなたがそれを取り込んだ直後にあなたのテキスト文字列を印刷していたら...あなたはあなたの質問を書く時間を節約したでしょう。 – GhostCat
Java GUIは、異なるロケールの異なるPLAFを使用して、異なるOS、画面サイズ、画面解像度などで動作する必要があります。したがって、ピクセルの完全なレイアウトには役立ちません。代わりに、レイアウトマネージャや[それらの組み合わせ](http://stackoverflow.com/a/5630271/418556)と[空白](http://stackoverflow.com/a/17874718/)のレイアウトパディングとボーダーを使用してください。 418556)。 –